社区
Java SE
帖子详情
String.replaceAll 的问题
realfish
2004-11-05 12:09:46
public class Test {
public static void main(String[] args) {
String temp = "$12345";
System.out.println(temp.replaceAll("$","0"));
}
}
为什么结果是$123450而不是我想要的012345;
用replace('$','0');就可以是012345?
...全文
154
11
打赏
收藏
String.replaceAll 的问题
public class Test { public static void main(String[] args) { String temp = "$12345"; System.out.println(temp.replaceAll("$","0")); } } 为什么结果是$123450而不是我想要的012345; 用replace('$','0');就可以是012345?
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
11 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
fog628
2004-11-05
打赏
举报
回复
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
上面的是String类replaceAll的代码,"$"在regular-expression(一个参数) 只是代表一行的结尾,用
"\\p{Punct}"代替"$"(要双"\\",和C一样)
public class Test {
public static void main(String[] args) {
String temp = "$12345";
System.out.println(temp.replaceAll("\\p{Punct}","0"));
}
}
PoemCode
2004-11-05
打赏
举报
回复
又学到了一点儿
haroyy
2004-11-05
打赏
举报
回复
String.replaceAll(String regex, String replacement);
regex是正则表达式
realfish
2004-11-05
打赏
举报
回复
treeroot(根根)
temp.replaceAll("\\$","0"));
$需要转义
可以的
dreamno
2004-11-05
打赏
举报
回复
转义哦。
treeroot
2004-11-05
打赏
举报
回复
在正则表达式中$表示一行的结尾
表许用\$转义才表示$字符
但是\又是字符串的转义字符,所以又要对它转义\\
结果就是 "\\$",这里经过了两次转义
treeroot
2004-11-05
打赏
举报
回复
temp.replaceAll("\\$","0"));
$需要转义
tomuno
2004-11-05
打赏
举报
回复
public class Test {
public static void main(String[] args) {
String temp = "$1234".substring(1,("$1234".length()-1));
System.out.println(temp);
}
}
treeroot
2004-11-05
打赏
举报
回复
用 replaceAll("\$","0")
lenoli
2004-11-05
打赏
举报
回复
就是就是,楼上正解
loverisyour
2004-11-05
打赏
举报
回复
replaceAll只能替换一些基本的字符,对特殊的字符它是不能替换的,结果也没什么规律。
【java】Java中的微优化--
String
.
replaceAll
在这篇文章中,我们将讨论另一种流行的代码结构
String
.
replaceAll
和
String
.replace方法的使用,我们将研究它如何影响 Java 11 中代码的性能以及您可以做些什么。 (请从性能的角度考虑以下所有代码) (请不要关注...
String
.replace()和
String
.
replaceAll
()性能对比
Java中有些常用的API其实值得仔细研究一下,比如
String
.replace()和
String
.
replaceAll
()。
JAVA
String
.
replaceAll
用法
正则表达式,关于
String
.
replaceALL
(..) class t1 { public static void main(
String
a[]) {
String
s = 'E:\\jbx\\x9\\io9';
String
ss; //把路径s中的'\'换为'\\',为何还要整8个'\'?我以为4个就行了。 ss = ...
Java
String
.
replaceAll
()方法
Java
String
.
replaceAll
()方法 Java
String
.
replaceAll
()方法用法实例教程, 给定的替换此字符串匹配给定的正则表达式替换每个子 描述 java.lang.
String
.
replaceAll
() 方法给定的替换此字符...
JavaScript
String
.prototype.
replaceAll
兼容性导致的
问题
见MDN
String
.prototype.
replaceAll
验证 查看运行出
问题
的浏览器的版本信息: chrome: firefox: 当然也可以通过界面去查看浏览器版本(firefox, chrome, …等不同厂商的浏览器,查看方式也不同) 解决 举个例子...
Java SE
62,616
社区成员
307,307
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章