«Back to Home

Core Java

Topics

String Class Methods In Java

String Class Methods

String class provides many methods to work on the strings.
 
With the help of these methods, we can perform many operations on the string such as trimming, concatenating, converting, comparing, replacing strings, formatting string, substring etc.
 
Java string is a robust and very useful concept because everywhere we use string. Everything is treated as a string, when we submit any form in Window based, Web based or mobile Application, we use the string.
 
Let's see some important methods of String class, which we use mostly in programming.
 
String toUpperCase() and toLowerCase() method in Java

In Java, string toUpperCase() method is used to convert the string into uppercase letter and string toLowerCase() method is used to convert the string into lowercase letter.
 
Let’s see an example, given below.
 
Code
  1. public class StringMethods {  
  2.     public static void main(String args[]) {  
  3.         String name = "Rocky";  
  4.         System.out.println(name.toUpperCase());  
  5.         System.out.println(name.toLowerCase());  
  6.         System.out.println(name);  
  7.     }  
  8. }  
1

Output

2

String trim() method in Java

In Java, string trim() method is used to eliminate the white spaces before and after the string.
 
Let’s see an example, given below.
 
Code
  1. public class StringMethods {  
  2.     public static void main(String args[]) {  
  3.         String animal = "        White Elephant        ";  
  4.         System.out.println(animal);  
  5.         System.out.println(animal.trim());  
  6.     }  
  7. }  
3
Output

4

String startsWith() and endsWith() method in Java

In Java, string startsWith() and endsWith method is used to checks if string starts or ends with given prefix. Hence, it returns true else it returns false.
 
Let’s see an example, given below.
 
Code
  1. public class StringMethods {  
  2.     public static void main(String args[]) {  
  3.         String n = "Marry";  
  4.         System.out.println(n.startsWith("Ma"));  
  5.         System.out.println(n.endsWith("r"));  
  6.         System.out.println(n.endsWith("y"));  
  7.     }  
  8. }  
5

Output

6

String charAt() method in Java

In Java, string charAt() method is used to return a character at particular index.
 
Let’s see an example, given below.
 
Code
  1. public class StringMethods {  
  2.     public static void main(String args[]) {  
  3.         String s = "Victoria";  
  4.         System.out.println(s.charAt(0));  
  5.         System.out.println(s.charAt(5));  
  6.         System.out.println(s.charAt(7));  
  7.     }  
  8. }  
7

Output

8

String length() method in Java

In Java, string length() method returns the length of the string.
 
Let’s see an example, given below.
 
Code
  1. public class StringMethods {  
  2.     public static void main(String args[]) {  
  3.         String name = "Alexander William";  
  4.         System.out.println(name.length());  
  5.     }  
  6. }  
9

Output

10

String intern() method in Java

In Java, a pool of strings is initially empty. It’s maintained privately by the class String.
 
If the intern method is called and the pool already contains a string equal to the String object as determined by the equals() method, the string from the pool is returned. The string object is added to the pool and a reference to the string object is returned.
 
Let’s see an example, given below.
 
Code
  1. public class StringMethods {  
  2.     public static void main(String args[]) {  
  3.         String toy = new String("Barbie Doll");  
  4.         String toy1 = toy.intern();  
  5.         System.out.println(toy);  
  6.     }  
  7. }  
11

Output

12

String valueOf() method in Java

In Java, this string method is used to change the given data type into the string.
 
Let’s see an example, given below.
 
Code
  1. public class StringMethods {  
  2.     public static void main(String args[]) {  
  3.         int a = 60;  
  4.         long b = 2035;  
  5.         String n = String.valueOf(a);  
  6.         System.out.println(n + 10);  
  7.     }  
  8. }  
13
Output

14

String replace() method in Java

In Java, this method is used to replace all the occurrences of the first order of character with the second order of character.
 
Let’s see an example, given below.
 
Code
  1. public class StringMethods {  
  2.     public static void main(String args[]) {  
  3.         String p = "I asked the Lord to send a friend.One chosen just for me… ";  
  4.         String p1 = p.replace("friend""somefriends");  
  5.         System.out.println(p1);  
  6.     }  
  7. }  
15

Output

16

Summary

Thus, we learnt that string class provides many methods to work on the strings and also learnt their usage.