将字符串的每个字母转换为 ascii 值 - java
Convert each letter of string into ascii values - java
我想添加两个字符串并删除字符串中的重复字符,然后我想将字符串中的每个字母转换为 ascii 值。转换后,我想添加每个字母的 ascii 值并获得值的总数。
例如:
ab+ab
ab // remove duplicates
65+66 //add ascii values
131 // total ascii values
添加两个字符串只是使用 + 运算符连接:
//first and second should be declared as String.
String both = first + second;
查看 here 了解如何从字符串中删除重复字符。
最后,一个字符的ascii值就是该字符的int值。因此,您可以这样做:
int asciiSum = 0;
for (int i = 0; i < both.length(); i++){
asciiSum += (int)both.charAt(i);
}
这将为您提供字符串中 ascii 值的总和。
希望对您有所帮助。
我想添加两个字符串并删除字符串中的重复字符,然后我想将字符串中的每个字母转换为 ascii 值。转换后,我想添加每个字母的 ascii 值并获得值的总数。 例如:
ab+ab
ab // remove duplicates
65+66 //add ascii values
131 // total ascii values
添加两个字符串只是使用 + 运算符连接:
//first and second should be declared as String.
String both = first + second;
查看 here 了解如何从字符串中删除重复字符。
最后,一个字符的ascii值就是该字符的int值。因此,您可以这样做:
int asciiSum = 0;
for (int i = 0; i < both.length(); i++){
asciiSum += (int)both.charAt(i);
}
这将为您提供字符串中 ascii 值的总和。
希望对您有所帮助。