String Methods in Java

length(), charAt()

Thu Jan 13, 2022

String Methods in Java

length()

length() method finds the length of a string i.e. the total number of characters present in the string.

Syntax 

public int length();    

String s1="Hello"; 

String s2="Java"; 

System.out.println("string length is: "+s1.length()); 

System.out.println("string length is: "+s2.length());

Output:

string length is: 5 

string length is: 4 

charAt() 

charAt() method returns a char value at the given index number. The index number starts from 0 and goes to n-1, where n is the length of the string. It returns StringIndexOutOfBoundsException, if the given index number is greater than or equal to this string length or a negative number. 

Syntax 

public char charAt(int index);

String name="Welcome"; 

char ch=name.charAt(3); 

System.out.println(ch); 

Output: 

c

S P SHARMA CLASSES
S P SHARMA SIR

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
S P SHARMA CLASSES 2024 Privacy policy Terms of use Contact us Refund policy