replace(char oldChar, char newChar)
返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有
oldChar 而生成的。
replaceAll
public String replaceAll(String
regex,
String replacement)使用给定的 replacement
字符串替换此字符串匹配给定的正则表达式的每个子字符串。
此方法调用的 str.replaceAll(regex, repl)
形式产生与以下表达式完全相同的结果:
Pattern.compile(regex).matcher(str).replaceAll(repl)
参数:
regex
- 用来匹配此字符串的正则表达式
返回:
得到的 String
抛出:
PatternSyntaxException -
如果正则表达式的语法无效
replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是:
1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(CharSequence即字符串序列的意思,说白了也就是字符串);
2)replaceAll的参数是regex,即基于规则表达式的替换,比如,可以通过replaceAll("\\d",
"*")把一个字符串所有的数字字符都换成星号;
相同点是都是全部替换,即把源字符串中的某一字符或字符串全部换成指定的字符或字符串,如果只想替换第一次出现的,可以使用
replaceFirst(),这个方法也是基于规则表达式的替换,但与replaceAll()不同的时,只替换第一次出现的字符串;
另外,如果replaceAll()和replaceFirst()所用的参数据不是基于规则表达式的,则与replace()替换字符串的效果是一样的,即这两者也支持字符串的操作;
还有一点注意:执行了替换操作后,源字符串的内容是没有发生改变的.
举例如下:
String src = new String("ab43a2c43d");
System.out.println(src.replace("3","f"));=>ab4f2c4fd.
System.out.println(src.replace('3','f'));=>ab4f2c4fd.
System.out.println(src.replaceAll("\\d","f"));=>abffafcffd.
System.out.println(src.replaceAll("a","f"));=>fb43fc23d.
System.out.println(src.replaceFirst("\\d,"f"));=>abf32c43d
System.out.println(src.replaceFirst("4","h"));=>abh32c43d.
分享到:
相关推荐
string.prototype.replaceall 用于String.prototype.replaceAll的ES Proposal规范填充程序。 如果不可用或不String.prototype....// replaceAll and replace are the same, when given a global regex to replace as
主要介绍了Java String.replace()方法"无效"的原因及解决方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
js里没有replaceAll的函数,需要我们自定义 代码如下: String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,”gm”),s2); } 2、使用js的replace进行处理 代码如下: str=str....
String.prototype.replaceAll建议 地位 冠军:Mathias Bynens(Google,@ mathiasbynens)。 该提案处于第4阶段,计划包含在ES2021中。 动机 (另请参见。) 当前,如果不使用全局正则表达式,就无法替换字符串...
方法: string.replace(new RegExp(oldString,”gm”),newString)) gm 分别代表: g=global, m=multiLine 大致上方法就是这样的,可以实现替换全部指定字串 另一个简单的验证JS的方法: 在浏览器地址栏输入 [removed]...
String.prototype.replaceAll = function(str1, str2) { return this.replace(new RegExp(str1, "gm"), str2); } // 清除空格 String.prototype.trim = function() { return this.replace(/^\s*(.*?)\s+$/, "$1");
主要介绍了C++中string替换所有指定字符串的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
"string".replaceAll(search: string, replace: string) "aaa" . replaceAll ( "a" , "b" ) ; // => bbb 对"aaa".replace("a", "b"); // => "baa"的修复"aaa".replace("a", "b"); // => "baa" "aaa".replace("a", ...
stringObj.replace(rgExp, replaceText) 参数 stringObj 必选项。要执行该替换的 String 对象或字符串文字。该字符串不会被 replace 方法修改。 rgExp 必选项。为包含正则表达式模式或可用标志的正则表达式对象...
String.prototype.replaceAll = function(reallyDo, replaceWith, ignoreCase) { if (!RegExp.prototype.isPrototypeOf(reallyDo)) { return this.replace(new RegExp(reallyDo, (ignoreCase ? “gi”: “g...
str = path.replace(this.rootPath.replaceAll("\\/", "\\\\"), "\\"); }else{ str = path.replace(this.rootPath, "/"); } //System.out.println("操作系统类型>>>>"+System.getProperty("os.name")); ...
replace() The replace() method returns the string that results when you replace text matching its first argument (a regular expression) with the text of the second argument (a string).
代码如下:String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,”gm”),s2); }; (function($){ /* * $-layer 0.1 – New Wave Javascript * * Copyright (c) 2008 King Wong * $Date...
代码如下:String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,”gm”),s2); }; (function($){ /* * $-layer 0.1 – New Wave Javascript * * Copyright (c) 2008 King Wong ...
代码如下:String.prototype.replaceAll = function(s1,s2){ return this.replace(new RegExp(s1,”gm”),s2); }; (function($){ /* * $-layer 0.1 – jquery pulg-in * * Copyright (c) 2008 King Wong * $Date: ...
目的是要实现一个非常快速的字符串替换功能,而不会出现基于RegExp的String.replace() 。 解决此问题的常用方法是不查找要查找的内容和要替换的内容,因为两者都处理特殊字符。 这种解决方案称为replaceAll()。 ...
replace() The replace() method returns the string that results when you replace text matching its first argument (a regular expression) with the text of the second argument (a string). If the g ...
这个工程简单写了如何使用replace替换字符串的反斜杠,有需要的可以下载
//10行代码搞定金额大小写转换//原创 渴死的鱼 hanlin2020@hotmail.com//改编 inRm inrm@263.... for i:= 0 to 11 do //去掉多余的零 result := StringReplace(result, cCha[0,i], cCha[1,i], [rfReplaceAll]);end;
private final String windowsParseMacAddress(String ipConfigResponse) throws java.text.ParseException { String localHost = null; try { localHost = java.net.InetAddress.getLocalHost().getHostAddress(); ...