Saturday 14 June 2014

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

public int codePointBefore(int index)
Returns the character (Unicode code point) before the specified index.

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

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

1. IndexOutOfBoundsException if the index is less than 1 or greater than the length of this string.

class StringCodePointBeforeIndexOut{
 public static void main(String args[]){
  String str = new String("Hello");
  
  System.out.println("str = " + str);
  System.out.println("Codepoint before 0th position is " + str.codePointBefore(0));
  
 }
}

Output
str = Hello
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 0
        at java.lang.String.codePointBefore(String.java:705)
        at StringCodePointBeforeIndexOut.main(StringCodePointBeforeIndexOut.java
:7)




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment