Friday 13 June 2014

String (char[] value, int offset, int count)

public String(char value[], int offset, int count)
Allocates a new String that contains characters from a subarray of the character array argument.

class StringConstructor9{
 public static void main(String args[]){
  char data[] = {'a', 'b', 'c', 'd'};
  String s1;

  s1 = new String(data, 2, 2);
  System.out.println(s1);  
 }
}

Output
cd

1. Throws IndexOutOfBoundsException If the offset and count arguments index characters outside the bounds of the value array.
class StringConstructor9IndexOut{
 public static void main(String args[]){
  char data[] = {'a', 'b', 'c', 'd'};
  String s1;

  s1 = new String(data, 2, 3);
  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:199)
        at StringConstructor9IndexOut.main(StringConstructor9IndexOut.java:6)



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment