我有一个字符串“speed,7,red,fast”。我想用字符串“七”替换 7。我该怎么做?
更多详细信息-
7 可以替换为任何字符串,而不仅仅是“Seven”。也可能是“七是天堂”。 我不想替换所有出现的 7。仅替换指定索引处的 7,即使用 7 的索引将 7 替换为某个字符串。
最佳答案
replaceAll("7", "Seven") //simple as that
编辑
然后您应该查找指定的索引。
String input = "test 7 speed,7,red,fast yup 7 tr";
int indexInteresdIn = 13;
if(input.charAt(indexInteresdIn) == '7'){
StringBuilder builder = new StringBuilder(input);
builder.replace(indexInteresdIn, indexInteresdIn+1, "Seven");
System.out.println(builder.toString());
}
关于java - 如何用字符串替换索引处的单个字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16274720/