Saturday 14 June 2014

codePointAt (int index) : get the code point at index

public int codePointAt(int index)
Returns the character (Unicode code point) at the specified index.

If the char value specified at the given index is in the high-surrogate range, the following index is less than the length of this String, and the char value at the following index is in the low-surrogate range, then the supplementary code point corresponding to this surrogate pair is returned. Otherwise, the char value at the given index is returned.

class StringCodePoint{
 public static void main(String args[]){
  String str = new String("Hello");
  
  System.out.println("str = " + str);
  System.out.println("Codepoint at 0th position is " + str.codePointAt(0));
  System.out.println("Codepoint at 1st position is " + str.codePointAt(1));
  System.out.println("Codepoint at 2nd position is " + str.codePointAt(2));
  System.out.println("Codepoint at 3rd position is " + str.codePointAt(3));
  System.out.println("Codepoint at 4th position is " + str.codePointAt(4));
 }
}

Output
str = Hello
Codepoint at 0th position is 72
Codepoint at 1st position is 101
Codepoint at 2nd position is 108
Codepoint at 3rd position is 108
Codepoint at 4th position is 111

Java implements UTF-16 encoding scheme. So Any unicode outside 0xFFFF is represented using surrogate scheme.


Example
class StringCodePoint1{
 public static void main(String args[]){
  int arr[] = {65536, 65537, 65538, 65539, 65540};
  String s = new String(arr, 0, arr.length);
  
  System.out.println("String length is " + s.length());
  for(int i =0; i < s.length(); i++){
   System.out.print("Code point at index " + i +" is ");
   System.out.println(s.codePointAt(i));
  }  
 }
}

Output
String length is 10
Code point at index 0 is 65536
Code point at index 1 is 56320
Code point at index 2 is 65537
Code point at index 3 is 56321
Code point at index 4 is 65538
Code point at index 5 is 56322
Code point at index 6 is 65539
Code point at index 7 is 56323
Code point at index 8 is 65540
Code point at index 9 is 56324

1. Throws IndexOutOfBoundsException if the index is negative or not less than the length of this string.
class StringCodePointIndexOut{
 public static void main(String args[]){
  String str = new String("Hello");
  
  System.out.println("str = " + str);
  System.out.println("Codepoint at -1 position is " + str.codePointAt(-1));
 }
}

Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
        at java.lang.String.codePointAt(String.java:675)
        at StringCodePointIndexOut.main(StringCodePointIndexOut.java:6)










Prevoius                                                 Next                                                 Home

No comments:

Post a Comment