by Dinesh Thakur

The String class provides methods for obtaining length, retrieving individual characters and concatenating strings. These include




int length (): This method returns the number of characters in a string. For example: The statement,

int k = sl.length();

will store 7 in a variable k i.e. the number of characters in the String object referenced by s1 ( i.e. Welcome).

char charAt (int index) : This method returns the character in the string at the specified index position. The index of the first character is 0, the second character is 1 and that of the last character is one less than the string length. The index argument must be greater than or equal to 0 and less than the length of the string. For example:


will return character 'e' i.e. character at index position 1 in the string referenced by s1.

String concat (String str): This method concatenates the specified string to the end of the string. For example : The statement,

sl.concat(" Everybody");

will concatenates the strings 'Welcome' and ' Everybody'.

public class StringMethods

      public static void main(String[] args)
        String s1 = "Welcome";
        System.out.println("s1.length() = "+ s1.length());
        System.out.println("s1.charAt(1) = "+ s1.charAt(1));
        System.out.println("s1.concat(\"Everybody\") = "+s1.concat(" Everyboday"));

Getting String Length, Characters and Concat Strings in Java