Friday 13 June 2014

String (int[] codePoints, int offset, int count)

public String(int[] codePoints, int offset, int count)
Allocates a new String that contains characters from a subarray of the Unicode code point array argument.

class StringConstructor10{
 public static void main(String args[]){
  int a[] = new int[26];
  int start = 97;
  
  for(int i=0; i < 26; i++){
   a[i] = start +i;
  }
  
  String s1 = new String(a, 0, 26);
  System.out.println(s1);
 }
}

Output
abcdefghijklmnopqrstuvwxyz

1. Throws IllegalArgumentException If any invalid Unicode code point is found in codePoints.
class StringConstructor10Illegal{
 public static void main(String args[]){
  int a[] = {12345678, 87654321, 123456789, 987654321};
  String s1 = new String(a, 0, 4);
  System.out.println(s1);
 }
}

Output
Exception in thread "main" java.lang.IllegalArgumentException: 12345678
        at java.lang.String.<init>(String.java:254)
        at StringConstructor10Illegal.main(StringConstructor10Illegal.java:6)

2. Throws IndexOutOfBoundsException, if the offset and count arguments index characters outside the bounds of the codePoints array.
class StringConstructor10IndexOut{
 public static void main(String args[]){
  int a[] = {97, 98, 99, 100};
  String s1 = new String(a, 0, 5);
  System.out.println(s1);
 }
}

Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
        at java.lang.String.<init>(String.java:241)
        at StringConstructor10IndexOut.main(StringConstructor10IndexOut.java:4)



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment