java替换和replaceAll.全部替换不起作用 [英] java replace e replaceAll. ReplaceAll not working
本文介绍了java替换和replaceAll.全部替换不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个练习.为什么不使用 replaceAll?我有一个错误:字符串索引超出范围:1
I have this exercise. Why not work with replaceAll? I have an error: String index out of range: 1
public class e3 {
public static void main(String[] args) {
String x="Sessione successiva";
String nuova=x.replace("i", "!");
String nuova2=nuova.replace("s", "$");
String nuova3=nuova2.replace("e", "&");
System.out.println(nuova3);
//replaceAll
String nuovaz=x.replaceAll("i", "!");
String nuova2z=nuovaz.replaceAll("s", "$");
String nuova3z=nuova2.replaceAll("e", "&");
System.out.println(nuova3z);
}
}
推荐答案
replaceAll
适用于正则表达式.在 replacAll
方法中,$
是一个保留字符,用于引用作为第一个参数给出的正则表达式中定义的组,因此您需要对其进行转义.
replaceAll
works with regular expressions. In the replacAll
method, $
is a reserved character used to reference groups defined in the regular expression given as first parameter, so you need to escape it.
String nuova2z=nuovaz.replaceAll("s", "\\$");
这篇关于java替换和replaceAll.全部替换不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文