by Dinesh Thakur Category: array

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

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles