我正在开发一个应用程序,我必须从一个音阶移调到另一个音阶。
例如:
cdefgab 为 gabcdef
c 为 g,d 为 a,e 为 b,f 为 c,g 为 d,a 为 e b 为 f
使用 .replace("c","g").replace("d","a").replace("e","b")... ###
但是输出是错误的,因为当第一个 c 被替换为 g , g 被替换为 d
代码如下:
String tune = "cdef gabc";
System.out.println(""+tune.replace("c","g")
.replace("d","a")
.replace("g","d"));
当前输出: 达夫达布德
要求的输出: 盖夫达布格
我希望第一个字符串 'c' 为 'g' 而不是 'd'
最佳答案
试试这个
System.out.println(tune.replace("a", "\uFFFF")
.replace("d", "a")
.replace("g", "d")
.replace("c", "g")
.replace("f", "c")
.replace("b", "f")
.replace("e", "b")
.replace("\uFFFF", "e"));
注意:\uFFFF 根据定义不是有效字符,因此它不会出现在有效字符串中。
关于java - 有没有一种方法可以在 Java 中更改具有相同字符的多个字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24514940/