![](/img/trans.png)
[英]Java-Android How to convert letters in a string to specified number
[英]Java - how to convert letters in a string to a number?
我对 Java 很陌生,所以我想知道如何将字符串中的字母转换为数字,例如hello world
将输出为8 5 12 12 15 23 15 18 12 4
。
所以a=1
, b=2
, z=26
等等。
由于这很可能是一项学习任务,我会给你一个提示:拉丁字母表中的所有 UNICODE 代码点都是按字母顺序排列的。 如果代码a
是一些数N
,然后的代码b
是N+1
,的代码c
是N+2
,等等; Z
的代码是N+26
。
您可以使用与减去整数相同的方式来减去字符代码点。 由于代码点是按字母顺序排列的,下面的计算
char ch = 'h';
int pos = ch - 'a' + 1;
产生h
的序列号,即8
。 如果您在循环中执行此计算,您将获得所需的结果。
请注意,上述公式仅适用于同一寄存器的字符。 如果你输入的字符串是大小写混合的,你需要在计算之前将每个字符都转换成小写,否则会出错。
String s = "hello world";
String t = "";
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
if (!t.isEmpty()) {
t += " ";
}
int n = (int)ch - (int)'a' + 1;
t += String.valueOf(n);
}
System.out.println(t);
这不涉及空间等。
public static void main(String[] args) {
String s = "hello world";
s = s.replace(" ", "");
char[] c = s.toCharArray();
for (Character ss : c)
System.out.println(ss - 'a' + 1);
}
对于位置 i 处的每个字符:输出 s.charAt(i)-'a'+1。 s 是字符串。
您可以执行以下操作:
for (int i = 0; i < a.length(); ++i) {
if (a.charAt(i) >= 'a' && a.charAt(i) <= 'z') {
System.out.println((int)a.charAt(i) - (int)'a');
}
}
使用一个 Map,键是字符,值是整数。 这不是一种有效的方法 - 地图应该是类的静态成员。
import java.util.HashMap;
import java.util.Map;
public class JavaApplication1
{
public static void main(String[] args)
{
final Map<Character, Integer> map;
final String str = "hello world";
map = new HashMap<>();
// or map = new HashMap<Character, Integer> if you are using something before Java 7.
map.put('a', 1);
map.put('b', 2);
map.put('c', 3);
map.put('d', 4);
map.put('e', 5);
map.put('f', 6);
map.put('g', 7);
map.put('h', 8);
map.put('i', 9);
map.put('j', 10);
map.put('k', 11);
map.put('l', 12);
map.put('m', 13);
map.put('n', 14);
map.put('o', 15);
map.put('p', 16);
map.put('q', 17);
map.put('r', 18);
map.put('s', 19);
map.put('t', 20);
map.put('u', 21);
map.put('v', 22);
map.put('w', 23);
map.put('x', 24);
map.put('y', 25);
map.put('z', 26);
for(final char c : str.toCharArray())
{
final Integer val;
val = map.get(c);
if(val == null)
{
// some sort of error
}
else
{
System.out.print(val + " ");
}
}
System.out.println();
}
}
如果您需要,如果您的字符串仅包含数字和字母,您可以使用以下测试代码将字符串转换为数字。
public static Long getNumericReferenceNumber(String str) {
String result = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isLetter(ch)) {
char initialCharacter = Character.isUpperCase(ch) ? 'A' : 'a';
result = result.concat(String.valueOf((ch - initialCharacter + 1)));
} else result = result + ch;
}
return Long.parseLong(result);
}
我添加了所有字符以获得公平的结果:
public static long stringToNumber(String s) {
long result = 0;
for (int i = 0; i < s.length(); i++) {
final char ch = s.charAt(i);
result += (int) ch;
}
return result;
}
将任何字符串视为以 x 为基数的数字,其中 x 是用于描述该字符串的字母表的长度,然后将该数字转换为另一个以 10 为基数的数字。谷歌搜索一种处理数字基数转换的算法。
如果我们采用英文键盘布局,这里的 x 可以等于 26+26+10+1(space)+y ,其中 y 是特殊字符的数量。
最常见的例子是从十六进制、以 16 为基数转换为十进制、以 10 为基数以及从二进制转换为十进制等等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.