ホームページ  >  記事  >  Java  >  JavaのreplaceAll()

JavaのreplaceAll()

王林
王林オリジナル
2024-08-30 15:38:10571ブラウズ

ReplaceAll() は、パラメータと一致する文字の出現をすべて置き換える String クラスのメソッドです。すべての部分文字列は、正規表現としてメソッドに渡した入力と指定された文字列の置換によって置き換えられます。このメソッドを開始すると、String オブジェクトが返されます。このパッケージの String クラス (java.lang.String) 内に存在します。このトピックでは、Java の replaceAll() について学びます。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

パラメータを含む構文

public String replaceAll(文字列正規表現、文字列置換)

上記は replaceAll() メソッドのメソッド シグネチャです。 String クラスによって提供される java in build メソッドにより、これをコード内で直接使用できます。入力として 2 つのパラメータを取ります:

  • regex(正規表現): この入力は、指定された文字列と一致するために使用されます。
  • replacement: これは、上記で一致した文字列の場所に必要な文字列として使用されます。これは、出力として表示したいと言える新しいコンテンツまたは文字列です。

このメソッドは常に文字列オブジェクトを返します。もう 1 つ、正規表現ではパターンも使用されます。これについては後で説明します。

結果の文字列: 出力として

Java では replaceAll() メソッドはどのように機能しますか?

replaceAll は String クラスに存在するメソッドです。正規表現と置換という 2 つのパラメータを入力として受け取ります。名前が示すように、文字列の一部または文字列全体を置き換えるのに使用されます。

このメソッドは以下の例外をスローします:

1. PatternSyntaxException

この例外は Java の未チェック例外で、メソッドに入力パラメータとして渡す正規表現にエラーがある場合にのみ発生します。他のクラスと同様に、問題を特定するのに役立つ事前定義または組み込みメソッドがあります。

  • public String getMessage(): このメソッドには例外の説明が含まれます。
  • public int getIndex(): このメソッドはエラーのインデックスを返します。
  • public String getPattern(): このメソッドは、エラーを含む正規表現を提供します。
  • public String getDescription(): このメソッドは、エラーに関する復号化を提供します。

2.正規表現の詳細

メソッドのパラメータに文字列として渡す正規表現ですが、この正規表現はパターン クラスのコンパイル済みインスタンスです。これは java.util.regex.Pattern パッケージに存在します。

この正規表現には次のものが含まれます:

  • パターン
  • マッチャー

これには matcher メソッドもあります。正規表現に準拠しています。

  • t: タブ用
  • a: 警告用
  • cx: 制御文字
  • e: エスケープ文字
  • n: 改行
  • f: フォームフィード

3.利用可能なメソッド

  • split(CharSequence input): このメソッドは string[] (文字列配列) を返し、分割したいものに代わって入力を受け取ります。
  • split(CharSequence input, int limit): 同じように動作しますが、このメソッドは制限パラメーターも受け取ります。
  • 静的パターンのコンパイル(String regex, int flags): このメソッドは正規表現とフラグの 2 つのパラメータを受け取り、正規表現をコンパイルします。
  • 4) 文字列パターン()
  • 5) 静的な文字列引用符(String s)

このパターン クラスはシリアル化可能なインターフェイスも実装します。

このようにして、replaceAll は Java で動作します。正規表現をコンパイルしたりその他の操作を行うために、パターンと内部一致が使用されます。

Java での replaceAll() の例

以下は、単一文字を置換する方法、正規表現と一致する文字列全体、完全な文字列から空白を削除する方法、および文字列を特殊文字で置換する方法を示すいくつかの例です。

例 #1

この例では、正規表現を (.*)java(.*) として渡し、部分文字列が java である文字列全体を最初から最後まで置き換えます。

コード:

import java.io.*;
public class DemoReg {
public static void main(String args[]) {
String str = new String("Example to show replace in java string.");
System.out.print("Resulted string after replace is  :" );
System.out.println(str.replaceAll("(.*)java(.*)", "replaced"));
}
}

出力:

JavaのreplaceAll()

例 #2

この例では、文字列の一部を特殊文字に置き換えています。つまり、文字列として扱えるものは何でも渡すことができます。数字を渡すこともできます。

コード:

public class DemoReg {
public static void main(String args[]) {
String s1="In this we are going to replace the string with some character. (repeat sequence)";
String str=s1.replaceAll("t*"," ***** ");
System.out.println("Ouptut is ::: ");
System.out.println(str);
}
}

出力:

JavaのreplaceAll()

Example #3

In this java class, we are replacing some part of the string with some other content. Example: “replacement done successfully” this in our case.

Code:

public class DemoReg {
public static void main(String args[]) {
String str="Now the demo is for replacing string with some another substring";
String result=str.replaceAll("string"," replacement done successfully");
System.out.println("Result after replace is :::: ");
System.out.println(result);
}
}

Output:

JavaのreplaceAll()

Example #4

In this example, we are trying to remove the spaces which are present in the given string. We have so many keywords with slashes (“\\”) that can be used to perform the given string’s operation.

Code:

public class DemoReg {
public static void main(String args[]) {
String str="Now we are going to replace the spaces present in the given string.";
System.out.println("String before replace performed :::: ");
System.out.println(str);
String result=str.replaceAll("\\s","");
System.out.println("Result after replace is :::: ");
System.out.println(result);
}
}

Output:

JavaのreplaceAll()

Example #5

In this java example, we are replacing the string with a single character only. It means when the given character appears in the string each time, it will be replaced by the method.

Code:

public class DemoReg {
public static void main(String args[]) {
String str="Replacing single character from the whole string demo.";
System.out.println("String before replace performed :::: ");
System.out.println(str);
String result=str.replaceAll("e"," X");
System.out.println("Result after replace is :::: ");
System.out.println(result);
}
}

Output:

JavaのreplaceAll()

We can have anything in the replacement, which is a string. But our regular expression has to be valid; otherwise, it will throw an unchecked exception for error containing regular expression in the replaceAll() method.

Conclusion

Replace method is the string class method of java, which takes two parameters. Any type of regular expression we can pass in it will replace the string for us unless it matches. So the above example will give you an understanding that how we can use this.

以上がJavaのreplaceAll()の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。