我需要在字符串中的每个给定字符后面插入一个空格。
例如“abc.def...”
需要变成“abc.def...”
所以在这种情况下给定的字符是点。
我在谷歌上的搜索没有找到这个问题的答案
我真的应该去学习一些严肃的正则表达式知识。
编辑:-------------------------------------------------------- -------------
String test = "0:;1:;";
test.replaceAll( "\\:", ": " );
System.out.println(test);
// output: 0:;1:;
// so didnt do anything
解决方案:-------------------------------------------------------- ----------
String test = "0:;1:;";
**test =** test.replaceAll( "\\:", ": " );
System.out.println(test);
最佳答案
您可以使用 String.replaceAll() :
String input = "abc.def...";
String result = input.replaceAll( "\\.", ". " );
// result will be "abc. def. . . "
编辑:
String test = "0:;1:;";
result = test.replaceAll( ":", ": " );
// result will be "0: ;1: ;" (test is still unmodified)
编辑:
正如其他答案中所述,String.replace()
就是这个简单替换所需的全部。仅当它是正则表达式时(就像您在问题中所说的那样),您才必须使用 String.replaceAll() 。
关于java - 在每个给定字符后插入一个空格 - java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21839902/