ReplaceAll() 是 String 类的方法,它替换所有与其参数匹配的字符,所有子字符串将被我们作为正则表达式传递给该方法的输入替换,并替换给定的盯着这个方法将返回我们 String 对象。它存在于String类(java.lang.String)这个包里面。在本主题中,我们将学习 Java 中的 ReplaceAll()。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
带参数的语法
公共字符串replaceAll(字符串正则表达式,字符串替换)
以上是replaceAll()方法的方法签名。由于String类提供了java in build方法,因此可以直接在我们的代码中使用。它需要两个参数作为输入:
此方法将始终返回字符串对象。还有一件事,正则表达式也使用一种模式,我们将在下面讨论。
结果字符串:作为输出
replaceAll 是 String 类中存在的方法。它需要两个参数作为输入,即正则表达式和替换。顾名思义,它用于替换字符串的某些部分或整个字符串。
此方法会抛出下面提到的异常:
这个异常是java中的未经检查的异常,只有当我们作为输入参数传入方法的正则表达式有错误时才会发生。与其他类一样,它有一些预定义或内置方法,可以帮助我们识别问题:
我们作为字符串传递到方法参数中的正则表达式,但这个正则表达式是模式类的编译实例。它存在于 java.util.regex.Pattern 包中。
这个正则表达式包含以下内容:
我们还有一个匹配器方法。它符合我们的正则表达式。
该模式类还实现了可序列化接口。
这样,replaceAll就可以在java中工作了。它在内部使用模式和匹配来编译正则表达式和其他操作。
下面的例子展示了如何使用它来替换单个字符、与正则表达式匹配的整个字符串、从整个字符串中删除空格以及用特殊字符替换字符串。
在此示例中,我们将正则表达式作为 (.*)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")); } }
输出:
在此示例中,我们用特殊字符替换字符串的一部分。这意味着我们可以传递任何可以被视为字符串的东西。我们也可以传递数字。
代码:
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); } }
输出:
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:
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:
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:
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.
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中文网其他相关文章!