C#中如何实现JAVA中的String.replaceAll()方法功能
2011-11-21 20:51
1096 查看
JDK原文注释:
String java.lang.String.replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expressionwith the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl)yields exactly the same result as the expression
Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see
Parameters:
其实,大体功能就是JAVA中的replaceAll(String regex, String replacement)方法,可以通过指定第一个参数“regex”——正则表达式或者需要替换的子字符串,第二个参数则是用于替换原串或与正则匹配的原串的字符串。
C#中,string 类 有Replace(string oldValue,string newValue)方法,可以与上述JAVA中的replaceAlll()方法中的“非正则匹配替换”功能相对应。
那么C#中如何实现“非正则匹配替换”功能呢?是否真的没得折腾了呢,哈哈,要相信在JAVA中有的,C#中大部分都能找到“映射”的。
肺话不多说,看看如何实现吧!
C#中的正则类Regex有个静态方法——“Replace(sting input,string pattern,string replacement)”可以实现上述功能。
C#源码:
string line = " 1 DA 232";
line=Regex.Replace(line.Trim(),"\\s+", " ");
JAVA源码:
String line = " 1 DA 232";
line = line.trim().replaceAll("\\s+", " ");
上述两种源码的最终实现的功能是一致的。
总结:JAVA中的replaceAll(String regex, String replacement)方法的功能,在C#中可以通过如下方式实现
A、string 类 有Replace(string oldValue,string newValue)方法,可以与上述JAVA中的replaceAlll()方法中的“非正则匹配替换”功能相对应。
B、正则类Regex有个静态方法Replace(sting input,string pattern,string replacement)”,可以实现上述“正则匹配替换”功能。
String java.lang.String.replaceAll(String regex, String replacement)
replaceAll
public String replaceAll(String regex, String replacement)
Replaces each substring of this string that matches the given regular expressionwith the given replacement.
An invocation of this method of the form str.replaceAll(regex, repl)yields exactly the same result as the expression
Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string; see
Matcher.replaceAll. Use
Matcher.quoteReplacement(java.lang.String)to suppress the special meaning of these characters, if desired.
Parameters:
regex- the regular expression to which this string is to be matched
replacement- the string to be substituted for each matchReturns:The resulting StringThrows:
PatternSyntaxException- if the regular expression's syntax is invalidSince:1.4See Also:
Pattern
其实,大体功能就是JAVA中的replaceAll(String regex, String replacement)方法,可以通过指定第一个参数“regex”——正则表达式或者需要替换的子字符串,第二个参数则是用于替换原串或与正则匹配的原串的字符串。
C#中,string 类 有Replace(string oldValue,string newValue)方法,可以与上述JAVA中的replaceAlll()方法中的“非正则匹配替换”功能相对应。
那么C#中如何实现“非正则匹配替换”功能呢?是否真的没得折腾了呢,哈哈,要相信在JAVA中有的,C#中大部分都能找到“映射”的。
肺话不多说,看看如何实现吧!
C#中的正则类Regex有个静态方法——“Replace(sting input,string pattern,string replacement)”可以实现上述功能。
C#源码:
string line = " 1 DA 232";
line=Regex.Replace(line.Trim(),"\\s+", " ");
JAVA源码:
String line = " 1 DA 232";
line = line.trim().replaceAll("\\s+", " ");
上述两种源码的最终实现的功能是一致的。
总结:JAVA中的replaceAll(String regex, String replacement)方法的功能,在C#中可以通过如下方式实现
A、string 类 有Replace(string oldValue,string newValue)方法,可以与上述JAVA中的replaceAlll()方法中的“非正则匹配替换”功能相对应。
B、正则类Regex有个静态方法Replace(sting input,string pattern,string replacement)”,可以实现上述“正则匹配替换”功能。
相关文章推荐
- C#中如何实现JAVA中的String.replaceAll()方法功能
- C#中如何实现JAVA中的String.replaceAll()方法功能
- 探究java中String.replaceAll方法把换行符(\n)替换为明文显示(\n)为何需要四个反斜杠(\\\\n)
- Java String.replaceAll()的方法
- Java替换字符串中的“\"”的处理方法(String.replaceAll的源码分析)
- JS正则表达式怎样实现Java中String.replaceAll的效果
- java replaceall方法替换String的几种特殊情况
- JAVA 1.3 中实现replaceAll 方法
- 在一个程序中需要用到全局变量(在多个class之间共享数据),请问如何定义具有这种功能的变量?或者是否有其他的方法解决多个class之间的数据共享(尽量简单实现)。 首先应该明确 Java中没有全局变
- Java String.replaceAll()方法
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- 如何用C# string.Replace方法将字符串中的替换成
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- Java+Selenium3框架设计篇4-如何实现登录功能放在setUp()方法中
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- JS正则表达式怎样实现Java中String.replaceAll的效果
- 如何将java中的"/"替换为"/"以及replaceAll(String regex, String replacement) 注意事项
- JavaScript的String类型replace()方法介绍和使用replace()方法实现简单html模板替换功能
- 详解Kotlin中如何实现类似Java或C#中的静态方法