Monday, 16 June 2014

copyValueOf (char[] data, int offset, int count)

public static String copyValueOf(char data[], int offset, int count)
Returns the string representation of a specific subarray of the char array argument. The offset argument is the index of the first character of the subarray. The count argument specifies the length of the subarray.

class StringCopyValueOfOffset{
 public static void main(String args[]){
  char a[] = {'w', 'x', 'y', 'z'};
  String str = String.copyValueOf(a, 1, 2);
  
  System.out.println("str = " + str);
 }
}

Output
str = xy

1. Throws IndexOutOfBoundsException if offset is negative, or count is negative, or offset+count is larger than data.length.

class StringCopyValueOfOffsetIndexOut{
 public static void main(String args[]){
  char a[] = {'w', 'x', 'y', 'z'};
  String str = String.copyValueOf(a, 1, 4);
  
  System.out.println("str = " + str);
 }
}

Output
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
        at java.lang.String.<init>(String.java:199)
        at java.lang.String.copyValueOf(String.java:3034)
        at StringCopyValueOfOffsetIndexOut.main(StringCopyValueOfOffsetIndexOut.
Java:4)


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment