用另一个字符串中的另一个字符替换字符串中的字符
Replacing a character in a string with another character from another string
我试图最终用另一组字符串替换一个句子。但是我在尝试用另一个字符串的另一个字符替换一个字符串中的一个字符时遇到了障碍。
这是我目前的情况。
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);
这种替换字符的方法是行不通的。有人可以帮助我吗?
原因
sentence.replace(sentence.charAt(i), encode.charAt(indexForEncode));
不起作用是因为 String
是 不可变的 (即,它们永远不会改变)。
所以,sentence.replace(...)
实际上并没有改变 sentence
;相反,它 returns 一个新的 String
。您需要编写 sentence = sentence.replace(...)
以在 sentence
.
中捕获该结果
好的,字符串 101:class 已关闭 (;->)。
综上所述,您真的不想继续将部分编码的 sentence
重新分配给它自己,因为几乎可以肯定,您会发现自己重新编码 [=14] 的字符=] 你已经编码了。最好保留 sentence
的原始形式,同时像这样一次一个字符地构建编码字符串:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sentence.length(); i++){
int indexForEncode = letters.indexOf(sentence.charAt(i));
sb.append(indexForEncode != -1
? encode.charAt(indexForEncode)
: sentence.charAt(i)
);
}
sentence = sb.toString();
我会按如下方式使用字符数组。对字符数组进行更改,然后使用 String.valueOf
获取字符串的新版本。
String letters = "abcdefghijklmnopqrstuvwxyz";
String encode = "kngcadsxbvfhjtiumylzqropwe";
// the sentence that I want to encode
String sentence = "hello, nice to meet you!";
char[] chars = sentence.toCharArray();
for (int i = 0; i < chars.length; i++){
int indexForEncode = letters.indexOf(sentence.charAt(i));
// if index is < 0, use original character, otherwise, encode.
chars[i] = indexForEncode < 0 ? chars[i] : encode.charAt(indexForEncode);
}
System.out.println(String.valueOf(chars));
版画
xahhi, tbga zi jaaz wiq!
您可以使用 codePoints
方法遍历此字符串中的字符,如果有的话,用另一个字符串中的字符替换它们。
public static void main(String[] args) {
String letters = "abcdefghijklmnopqrstuvwxyz";
String encode = "kngcadsxbvfhjtiumylzqropwe";
String sentence = "hello, nice to meet you!";
String encoded = replaceCharacters(sentence, letters, encode);
String decoded = replaceCharacters(encoded, encode, letters);
System.out.println(encoded); // xahhi, tbga zi jaaz wiq!
System.out.println(decoded); // hello, nice to meet you!
}
public static String replaceCharacters(String text, String from, String to) {
// wrong cipher, return unencrypted string
if (from.length() != to.length()) return text;
// IntStream over the codepoints of this text string
return text.codePoints()
// Stream<Character>
.mapToObj(ch -> (char) ch)
// encrypt characters
.map(ch -> {
// index of this character
int i = from.indexOf(ch);
// if not present, then leave it as it is,
// otherwise replace this character
return i < 0 ? ch : to.charAt(i);
}) // Stream<String>
.map(String::valueOf)
// concatenate into a single string
.collect(Collectors.joining());
}
另请参阅:
我试图最终用另一组字符串替换一个句子。但是我在尝试用另一个字符串的另一个字符替换一个字符串中的一个字符时遇到了障碍。
这是我目前的情况。
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);
这种替换字符的方法是行不通的。有人可以帮助我吗?
原因
sentence.replace(sentence.charAt(i), encode.charAt(indexForEncode));
不起作用是因为 String
是 不可变的 (即,它们永远不会改变)。
所以,sentence.replace(...)
实际上并没有改变 sentence
;相反,它 returns 一个新的 String
。您需要编写 sentence = sentence.replace(...)
以在 sentence
.
好的,字符串 101:class 已关闭 (;->)。
综上所述,您真的不想继续将部分编码的 sentence
重新分配给它自己,因为几乎可以肯定,您会发现自己重新编码 [=14] 的字符=] 你已经编码了。最好保留 sentence
的原始形式,同时像这样一次一个字符地构建编码字符串:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < sentence.length(); i++){
int indexForEncode = letters.indexOf(sentence.charAt(i));
sb.append(indexForEncode != -1
? encode.charAt(indexForEncode)
: sentence.charAt(i)
);
}
sentence = sb.toString();
我会按如下方式使用字符数组。对字符数组进行更改,然后使用 String.valueOf
获取字符串的新版本。
String letters = "abcdefghijklmnopqrstuvwxyz";
String encode = "kngcadsxbvfhjtiumylzqropwe";
// the sentence that I want to encode
String sentence = "hello, nice to meet you!";
char[] chars = sentence.toCharArray();
for (int i = 0; i < chars.length; i++){
int indexForEncode = letters.indexOf(sentence.charAt(i));
// if index is < 0, use original character, otherwise, encode.
chars[i] = indexForEncode < 0 ? chars[i] : encode.charAt(indexForEncode);
}
System.out.println(String.valueOf(chars));
版画
xahhi, tbga zi jaaz wiq!
您可以使用 codePoints
方法遍历此字符串中的字符,如果有的话,用另一个字符串中的字符替换它们。
public static void main(String[] args) {
String letters = "abcdefghijklmnopqrstuvwxyz";
String encode = "kngcadsxbvfhjtiumylzqropwe";
String sentence = "hello, nice to meet you!";
String encoded = replaceCharacters(sentence, letters, encode);
String decoded = replaceCharacters(encoded, encode, letters);
System.out.println(encoded); // xahhi, tbga zi jaaz wiq!
System.out.println(decoded); // hello, nice to meet you!
}
public static String replaceCharacters(String text, String from, String to) {
// wrong cipher, return unencrypted string
if (from.length() != to.length()) return text;
// IntStream over the codepoints of this text string
return text.codePoints()
// Stream<Character>
.mapToObj(ch -> (char) ch)
// encrypt characters
.map(ch -> {
// index of this character
int i = from.indexOf(ch);
// if not present, then leave it as it is,
// otherwise replace this character
return i < 0 ? ch : to.charAt(i);
}) // Stream<String>
.map(String::valueOf)
// concatenate into a single string
.collect(Collectors.joining());
}
另请参阅: