首页 > Java > java教程 > 正文

Java中的replaceAll()

王林
发布: 2024-08-30 15:38:10
原创
711 人浏览过

ReplaceAll() 是 String 类的方法,它替换所有与其参数匹配的字符,所有子字符串将被我们作为正则表达式传递给该方法的输入替换,并替换给定的盯着这个方法将返回我们 String 对象。它存在于String类(java.lang.String)这个包里面。在本主题中,我们将学习 Java 中的 ReplaceAll()。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

带参数的语法

公共字符串replaceAll(字符串正则表达式,字符串替换)

以上是replaceAll()方法的方法签名。由于String类提供了java in build方法,因此可以直接在我们的代码中使用。它需要两个参数作为输入:

  • regex(正则表达式):此输入用于匹配给定的字符串。
  • replacement:这用作我们想要的字符串,代替上面匹配的字符串。这是我们希望将其视为输出的新内容或字符串。

此方法将始终返回字符串对象。还有一件事,正则表达式也使用一种模式,我们将在下面讨论。

结果字符串:作为输出

Java中的replaceAll()方法是如何工作的?

replaceAll 是 String 类中存在的方法。它需要两个参数作为输入,即正则表达式和替换。顾名思义,它用于替换字符串的某些部分或整个字符串。

此方法会抛出下面提到的异常:

1. PatternSyntaxException

这个异常是java中的未经检查的异常,只有当我们作为输入参数传入方法的正则表达式有错误时才会发生。与其他类一样,它有一些预定义或内置方法,可以帮助我们识别问题:

  • public String getMessage():该方法包含异常的描述。
  • public int getIndex():此方法将返回错误的索引。
  • public String getPattern():该方法将为我们提供包含错误的正则表达式。
  • public String getDescription():该方法将为我们提供有关错误的解密。

2.正则表达式详细信息

我们作为字符串传递到方法参数中的正则表达式,但这个正则表达式是模式类的编译实例。它存在于 java.util.regex.Pattern 包中。

这个正则表达式包含以下内容:

  • 图案
  • 匹配器

我们还有一个匹配器方法。它符合我们的正则表达式。

  • t:用于选项卡
  • a:用于警报
  • cx:控制字符
  • e:转义字符
  • n:换行
  • f:换页

3.可用方法

  • split(CharSequence input):该方法返回 string[] (字符串数组)并代表我们要分割的输入。
  • split(CharSequence input, int limit):工作原理相同,但该方法也接受一个 limit 参数。
  • 静态模式编译(String regex,int flags):此方法采用两个参数,正则表达式和标志,并编译我们的正则表达式。
  • 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中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!