Java equivalent for .charCodeAt()

syb0rg picture syb0rg · Dec 31, 2012 · Viewed 16.2k times · Source

In JavaScript, .charCodeAt() returns a Unicode value at a certain point in the string which you pass to a function. If I only had one character, I could use the code below to get the Unicode value in Java.

public int charCodeAt(char c) {
     int x;
     return x = (int) c;
}

If I had a string in Java, how would I get the Unicode value of one individual character within the string, like the .charCodeAt() function does for JavaScript?

Answer

jlordo picture jlordo · Dec 31, 2012

Java has the same method: Character.codePointAt(CharSequence seq, int index);

String str = "Hello World";
int codePointAt0 = Character.codePointAt(str, 0);