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.

toUpperCase()

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

Example
snippet
String s="Sachin";
   System.out.println(s.toUpperCase());//SACHIN
   System.out.println(s);//Sachin(no change in original)
Output
SACHIN sachin Sachin
toLowerCase()

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

Example
snippet
String s="Sachin";
   System.out.println(s.toLowerCase());//sachin
   System.out.println(s);//Sachin(no change in original)
Output
sachin Sachin
trim()

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

Example
snippet
String s="  Sachin  ";
   System.out.println(s);//  Sachin  
   System.out.println(s.trim());//Sachin
Output
Sachin Sachin
startsWith()
Example
snippet
String s="Sachin";
   System.out.println(s.startsWith("Sa"));//true
Output
true
endsWith()
Example
snippet
String s="Sachin";
   System.out.println(s.endsWith("n"));//true
Output
true
charAt()

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

Example
snippet
String s="Sachin";
   System.out.println(s.charAt(0));//S
   System.out.println(s.charAt(3));//h
Output
S h
length()

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

Example
snippet
String s="Sachin";
   System.out.println(s.length());//6
Output
6
intern()

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.

Example
snippet
String s=new String("Sachin");
   String s2=s.intern();
   System.out.println(s2);//Sachin
Output
Sachin
valueOf()

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

Example
snippet
int a=10;
  String s=String.valueOf(a);
  System.out.println(s+10);
Output
1010
replace()

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

Example
snippet
String s1="Java is a programming language. Java is a platform. Java is an Island.";  
String replaceString=s1.replace("Java","Kava");//replaces all occurrences of "Java" to "Kava"  
System.out.println(replaceString);
Output
Kava is a programming language. Kava is a platform. Kava is an Island.
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +