Java - how to convert letters in a string to a number?

前端 未结 8 1942
我在风中等你
我在风中等你 2020-12-10 02:33

I\'m quite new to Java so I am wondering how do you convert a letter in a string to a number e.g. hello world would output as 8 5 12 12 15 23 15 18 12 4

8条回答
  •  伪装坚强ぢ
    2020-12-10 03:16

    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);
    }
    

提交回复
热议问题