C#中如何实现JAVA中的String.replaceAll()方法功能
2012-01-09 22:52
896 查看
jdk原文注释:
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
pattern.
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:regex- the regular expression to which this string is to be matchedreplacement- 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 = " 1da232";
line=regex.replace(line.trim(),"\\s+", " ");
java源码:
string line = " 1da232";
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)”,可以实现上述“正则匹配替换”功能。
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定
这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
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
pattern.
compile(regex).
matcher(str).
replaceall(repl)
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 matchedreplacement- 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 = " 1da232";
line=regex.replace(line.trim(),"\\s+", " ");
java源码:
string line = " 1da232";
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)”,可以实现上述“正则匹配替换”功能。
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定
这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/
相关文章推荐
- C#中如何实现JAVA中的String.replaceAll()方法功能
- C#中如何实现JAVA中的String.replaceAll()方法功能
- java、php、C#、asp实现短信群发功能的方法
- JS正则表达式怎样实现Java中String.replaceAll的效果
- java servlet 几种页面跳转的方法,需要的朋友可以参考一下 在访问网页的过程中,页面自动刷新、跳转和重定向是经常用到的,这里就说说在Java中是如何实现这些功能的! 一、页面自动刷新
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- java string 转date方法如何实现
- Java String.replaceAll()的方法
- 探究java中String.replaceAll方法把换行符(\n)替换为明文显示(\n)为何需要四个反斜杠(\\\\n)
- Java替换字符串中的“\"”的处理方法(String.replaceAll的源码分析)
- JS正则表达式怎样实现Java中String.replaceAll的效果
- JAVA 1.3 中实现replaceAll 方法
- 在一个程序中需要用到全局变量(在多个class之间共享数据),请问如何定义具有这种功能的变量?或者是否有其他的方法解决多个class之间的数据共享(尽量简单实现)。 首先应该明确 Java中没有全局变
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- Java String.replaceAll()方法
- java replaceall方法替换String的几种特殊情况
- Java+Selenium3框架设计篇4-如何实现登录功能放在setUp()方法中
- Java中利用MessageFormat对象实现类似C# string.Format方法格式化
- 如何用C# string.Replace方法将字符串中的替换成