Different methods of string

clone()

The easiest method to copy an array, if what we want is to exactly copy the entire array, is to use the clone() method of the source array Object. Here is the signature of the method.

Output:

split()

split method takes string as input and return string array.

Output:

toString

he simplest way to convert an Array to String is by using the java.util.Arrays class. It has already a built in method toString().

Output:

length and length()

  • length variable is used to calculate size of array.
  • length() method is use to calculate size of string. Returns number of chars present in String.

Output:

charAt()

charAt() method is used to find a character at index in string. charAt() method takes index as input. If the entered index is more than size of string then it throws “StringIndexOutOfBound” exception.

Output

If index is more than size of string

Output

Help others by sharing the content!

Difference between length and length( )

  • length variable is used to calculate size of array.
  • length() method is use to calculate size of string. Returns number of chars present in String.

Output:

Help others by sharing the content!