Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent for .charCodeAt()

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?

like image 433
syb0rg Avatar asked Dec 31 '12 17:12

syb0rg


People also ask

What is charCodeAt in Java?

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.

What is the use of charCodeAt ()?

The charCodeAt() method returns an integer between 0 and 65535 representing the UTF-16 code unit at the given index.


1 Answers

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

String str = "Hello World";
int codePointAt0 = Character.codePointAt(str, 0);
like image 157
jlordo Avatar answered Sep 21 '22 21:09

jlordo