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?
The charCodeAt() method returns the Unicode of the character at a specified index (position) in a string. The index of the first character is 0, the second is 1, .... The index of the last character is string length - 1 (See Examples below). See also the charAt() method.
The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.
Java has the same method: Character.codePointAt(CharSequence seq, int index);
String str = "Hello World";
int codePointAt0 = Character.codePointAt(str, 0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With