【发布时间】:2021-06-15 09:43:32
【问题描述】:
我试图最终用另一组字符串替换一个句子。但是我在尝试用另一个字符串的另一个字符替换字符串中的字符时遇到了障碍。
这是我目前所拥有的。
String letters = "abcdefghijklmnopqrstuvwxyz";
String encode = "kngcadsxbvfhjtiumylzqropwe";
// the sentence that I want to encode
String sentence = "hello, nice to meet you!";
//swapping each char of 'sentence' with the chars in 'encode'
for (int i = 0; i < sentence.length(); i++) {
int indexForEncode = letters.indexOf(sentence.charAt(i));
sentence.replace(sentence.charAt(i), encode.charAt(indexForEncode));
}
System.out.println(sentence);
这种替换字符的方式行不通。有人可以帮我吗?
【问题讨论】: