Java String Class Methods

The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java. By the help of these methods, we can perform operations on String objects 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 use some 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.

Stringoperation1.java

Test it Now

Output:

SACHIN
sachin
Sachin

Java String trim() method

The String class trim() method eliminates white spaces before and after the String.

Stringoperation2.java

Test it Now

Output:

Sachin  
Sachin

Java String startsWith() and endsWith() method

The method startsWith() checks whether the String starts with the letters passed as arguments and endsWith() method checks whether the String ends with the letters passed as arguments.

Stringoperation3.java

Test it Now

Output:

true
true

Java String charAt() Method

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

Stringoperation4.java

Test it Now

Output:

S
h

Java String length() Method

The String class length() method returns length of the specified String.

Stringoperation5.java

Test it Now

Output:

6

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.

Stringoperation6.java

Test it Now

Output:

Sachin

Java String valueOf() Method

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

Stringoperation7.java

Output:

1010

Java String replace() Method

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

Stringoperation8.java

Output:

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





Latest Courses