Java String replaceAll 方法

在 Java 编程中,replaceAll 函数是字符串方法之一,可用于查找与正则表达式匹配的每个子字符串,并将其替换为新指定的文本。替换文本是日常编码中的基本任务之一,replaceAll() 可以轻松完成此任务并返回一个全新的字符串。例如,在撰写关于 USA 的文档后,如果您想将“usa”替换为“USA”,则可以使用 replaceAll() 函数。

通常,字符串用于存储文本信息。Java replaceAll() 函数提供了多种选项,用于使用正则表达式替换单个字符、多个字符或字符串模式匹配。您可以使用此函数查找所需的模式并将其替换为您自己的子字符串。

本文将向您展示如何在 Java 编程语言中编写 string.repalceAll 方法、其语法、参数以及多个示例。

Java String replaceAll 语法

此编程语言提供了 replaceAll 方法,用于将匹配的子字符串替换为用户指定的字符串。replaceAll 方法接受两个字符串参数。第一个参数 (regex) 是表示 String_Object 中存在的子字符串的正则表达式,第二个参数 (Replacement) 是您想要替换的新子字符串。

public String replaceAll(String Regexp, String Replacement);

//To use in program
String_Object.replaceAll(Regexp, Replacement);

String_Object:请指定要执行替换的有效对象。

Replacement:请指定要插入 String_Object 的有效新字符串。

Regexp:请指定我们要更改的有效字符串或表示该字符串的正则表达式。此 Java replaceAll 方法将用新字符串 (Replacement) 替换您在此处放置的任何内容。

regex(正则表达式)参数非常强大。您可以使用它来查找文本中的复杂模式。

  • \A: 字符串的开头。
  • \d: 匹配数字。
  • \D: 检查非数字字符。
  • \s: 匹配空格。
  • \S: 查找非空格字符。
  • \w: 匹配单词字符。
  • \W: 匹配非单词字符。
  • \z: 字符串的结尾。

replaceAll 将匹配的子字符串替换为用户指定的字符串对象。在下面的 Java 程序中,我们将子字符串用作第一个参数。这有助于理解 replaceAll 方法的基本功能。

Java String replaceAll 示例:替换单个字符

本节演示如何使用 replaceAll() 函数替换字符串中的特定字符。在此示例中,原始文本 str 在多个位置包含“a”。此函数匹配所有出现的“a”并将其替换为“H”。

public class Simple {
	public static void main(String[] args) {
		String str = "Tutorials about a Programming";
			
		String reText = str.replaceAll("a", "H");

		System.out.println(reText);

	}
}
TutoriHls Hbout H ProgrHmming

replaceAll 函数替换单词

除了替换单个字符外,您还可以使用此 Java 字符串 replaceAll 函数来更改整个单词或句子。

public class Simple {
	public static void main(String[] args) {
		String str = "We are abc group working at abc company";
			
		String reText = str.replaceAll("abc", "MNC");
		String wrongText = str.replaceAll("abZ", "MNC");

		System.out.println(reText);
		System.out.println(wrongText);
	}
}
We are MNC group working at MNC company
We are abc group working at abc company

此示例中的下一条 Java 语句在原始文本中查找“abc”单词,并将整个术语替换为给定的子字符串 (MNC)。

 reText = str.replaceAll("abc", "MNC")

在下一行中,我们尝试将不存在的子字符串“abZ”替换为子字符串“MNC”。上面的截图显示它没有进行任何替换(String_Object 未更改)。

wrongText = str.replaceAll("abZ", "MNC");

最后,我们使用 System.out.println 语句打印输出。

Java String replaceAll 函数替换多个字符

此示例使用 regex 或正则表达式查找多个字符,即 a 和 e,并将它们替换为 # 符号。

public class Simple {
	public static void main(String[] args) {
		String str = "we are abc working in abc companies";
			
		String reText = str.replaceAll("[ae]", "#");

		System.out.println(reText);
	}
}
w# #r# #bc working in #bc comp#ni#s

replaceAll:不区分大小写的替换

此函数默认区分大小写,因此它将 a 和 A 视为不同。您必须使用 (?i) 正则表达式和子字符串来实现不区分大小写的替换。

在下面的示例中,我们使用 (“(?i)t”, “A”) 来指示 replaceAll() 函数搜索小写和大写的 t(不区分大小写),然后将它们替换为 A。

public class Simple {
	public static void main(String[] args) {
		String str = "Tutorial GaTeway Tweets";
			
		String reText = str.replaceAll("t", "A");
		String newText = str.replaceAll("(?i)t", "A");

		System.out.println(reText);
		System.out.println(newText);
	}
}
TuAorial GaTeway TweeAs
AuAorial GaAeway AweeAs

Java string replaceAll 方法移除空格

在此示例中,“\s”用于识别给定字符串中的空格,我们将它们替换为空格。

public class Simple {
	public static void main(String[] args) {
		String str = "Hi, hello. Welcome to You!";
			
		String reText = str.replaceAll("\\s", "");

		System.out.println(reText);
	}
}
Hi,hello.WelcometoYou!

如何在字符之间添加空格?

只需识别每个字符并用空格替换它们即可。

public class Simple {
	public static void main(String[] args) {
		String str = "TutorialGateway";
			
		String reText = str.replaceAll("", " ");

		System.out.println(reText);
	}
}
 T u t o r i a l G a t e w a y

replaceAll 移除非数字字符

在此示例中,“\D”用于识别给定字符串中的非数字字符,我们将使用 replaceAll 函数将其删除。

public class Simple {
	public static void main(String[] args) {
		String strNum = "Hi 10, How 20 and 30 are doing!";
		String phNum = "(+91)987654321";
			
		String reText = strNum.replaceAll("\\D", "");
		String newText = phNum.replaceAll("\\D", "");

		System.out.println(reText);
		System.out.println(newText);
	}
}
102030
91987654321

Java String replaceAll 正则表达式示例

程序 将帮助您实际理解在 string replaceAll 方法中使用正则表达式。

package StringFunctions;

public class ReplaceAllMethod2 {
	public static void main(String[] args) {
		String str = "We are abc working at Abc";
		String strd = "Hi 10, How 20 and 30 are doing!";
		
		String str2 = strd.replaceAll("[0-9]+", "");
		String str3 = strd.replaceAll(" [0-9]+", "");
		String str4 = str.replaceAll("[a-zA-Z]+", "Java");

		System.out.println("After Replacing First Sub String in str = " + str2);
		System.out.println("After Replacing Last Sub String in str = " + str3);
		System.out.println("After Replacing First Sub String in str1 = " + str4);
	}
}
Java String replaceAll Method 2

以下语句将调用 replaceAll 方法来查找子字符串 ‘[0-9]’ 并将其替换为“”。它将把 0 到 9 之间的数字替换为空格。

str2 = strd.replaceAll("[0-9]+", "");

上面的语句返回的输出包含额外的空格。因此,我们对上述语句进行了修改以消除额外的空格。

str3 = strd.replaceAll(" [0-9]+", "");

以下 String 函数 语句将查找子字符串 ‘[a-zA-Z]’ 并将其替换为“Java”。它将把 a 到 z、A 到 Z 之间的字母替换为 Java。

str4 = str.replaceAll("[a-zA-Z]+", "Java");

如果您在上面的示例中使用 [^a-zA-Z0-9] 正则表达式,您可以替换所有非字母数字字符。这里,^ 告知 replaceAll 函数替换任何不在 a-z、A-Z 和 0-9 范围内的字符。

Java string replaceAll 中的转义字符

在正则表达式中,元字符具有特殊含义,因此您不能直接在 replaceAll() 函数中使用它们。元字符是“\ ^ $ . | ? + * [] {} ()”。

如前所述,此函数接受 regex 作为第一个参数。因此,要在第一个参数中使用元字符,您必须使用 \\ 转义这些字符。

在下面的示例中,原始文本 strMeta 包含一个“ 符号,即元字符。因此,我们使用 \ 来转义该字符,并将每个 * 替换为 @ 符号。

public class Simple {
	public static void main(String[] args) {
		String strMeta = "Hi * Guys *, How * and * are * doing!";
			
		String reText = strMeta.replaceAll("\\*", "@");

		System.out.println(reText);
	}
}
Hi @ Guys @, How @ and @ are @ doing!

评论已关闭。