Methods of String class

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java String class methods

The java.lang.String class provides a lot of methods to work on string. By the help of these methods, we can perform operations on string such as trimming, concatenating, converting, comparing, replacing strings etc.

Java String is a powerful concept because everything is treated as a string if you submit any form in window based, web based or mobile application.

Let's see the important methods of String class.

Java String toUpperCase() and toLowerCase() method

The java string toUpperCase() method converts this string into uppercase letter and string toLowerCase() method into lowercase letter.

  1. tring s="Sachin";  
  2. System.out.println(s.toUpperCase());//SACHIN  
  3. System.out.println(s.toLowerCase());//sachin  
  4. System.out.println(s);//Sachin(no change in original)  

Test it Now


Java String trim() method                   

The string trim() method eliminates white spaces before and after string.

  1. String s="  Sachin  ";  
  2. System.out.println(s);//  Sachin    
  3. System.out.println(s.trim());//Sachin  

Test it Now


Java String startsWith() and endsWith() method

  1. String s="Sachin";  
  2.  System.out.println(s.startsWith("Sa"));//true  
  3.  System.out.println(s.endsWith("n"));//true  

Test it Now


Java String charAt() method

The string charAt() method returns a character at specified index.

  1. String s="Sachin";  
  2. System.out.println(s.charAt(0));//S  
  3. System.out.println(s.charAt(3));//h  

Test it Now


Java String length() method

  1. String s="Sachin";  
  2. System.out.println(s.length());//6  

Test it Now


The string length() method returns length of the string.

Java String intern() method

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

  1. String s=new String("Sachin");  
  2. String s2=s.intern();  
  3. System.out.println(s2);//Sachin  

Test it Now


Java String valueOf() method

The string valueOf() method coverts given type such as int, long, float, double, boolean, char and char array into string

  1. int a=10;  
  2. String s=String.valueOf(a);  
  3. System.out.println(s+10);  



Java String replace() method

The string replace() method replaces all occurrence of first sequence of character with second sequence of character

  1. tring s1="Java is a programming language. Java is a platform. Java is an Island.";    
  2. String replaceString=s1.replace("Java","Kava");//replaces all occurrences of "Java" to "Kava"    
  3. System.out.println(replaceString);    


Kava is a programming language. Kava is a platform. Kava is an Island.