Find ascii value of the character at specific position in a String
codePointAt(int position) method of String class can be used to find the ascii value of a character at specific position in a String.
Return type of codePointAt(int position) is int.
Index or position is counted from 0 to length-1 characters. i.e 0 th index is taken as first character and last character is length-1.
Example (finding ascii value of second character in a String)
class StringTest{
public static void main(String args[]){
String str=new String("A2N");
int secondcharascii=str.codePointAt(1);
System.out.println("Ascii value of second character is "+secondcharascii);
}
}
Output
Ascii value of second character is 50
codePointBefore(int position) method of String class can be used to find the ascii value of a character before the specific position in a String.
Return type of codePointBefore(int position) is int.
Example (finding ascii value of the character before second character in a String)
class StringTest{
public static void main(String args[]){
String str=new String("A2N");
int bcharascii=str.codePointBefore(1);
System.out.println("Ascii value of the character before second character is "+bcharascii);
}
}
Output
Ascii value of the character before second character is 65