In Java, String plays a very important role. It represents a sequence of characters and is widely used for storing and manipulating text. We can think of a string as a bunch of characters, which are put together like a sentence or a word. In simple words, a string is like an array of characters, but unlike arrays, it is immutable and comes with many built-in methods in Java. With the help of these methods, we can perform various operations such as concatenation, comparison, and manipulation.
In this article, we are going to discuss the most commonly used Java String methods.
What is a String in Java?
Before discussing about String methods, let us first know what a string is in Java. A string is a sequence of characters.
Note: In Java, objects of the String class are immutable, which simply means they cannot be changed once created.
Example:
Java
// Java Program to demonstrate String
public class Geeks {
// Main Function
public static void main(String args[])
{
// creating Java string using a new keyword
String str = new String("Geeks");
System.out.println(str);
}
}
The image below explanins how strings are stored in C/C++. Each character in the string "Geeks" is stored in contiguous memory locations and ends with a null character \0, which signals the end of the string.
In Java, strings are objects backed by an internal character array, but they are not null-terminated. Java maintains the length of the string internally, so it does not rely on a special character like \0 to detect the end of the string.

Commonly Used Java String Methods
The most commonly used methods are listed below.
String Methods | Description |
---|
| Returns the number of characters in the String. |
---|
| Returns the character at ith index. |
---|
| Return the substring from the ith index character to end. |
---|
| Returns the substring from i to j-1 index. |
---|
| Concatenates specified string to the end of this string. |
---|
| Finds the position of the first occurrence of the given substring within the main string. If the specified string s is not found in the input string, the method returns -1 by default. |
---|
| Returns the index within the string of the first occurrence of the specified string, starting at the specified index. |
---|
| Returns the index within the string of the last occurrence of the specified string. If the specified string s is not found in the input string, the method returns -1 by default. |
---|
| Compares this string to the specified object. |
---|
| Compares string to another string, ignoring case considerations. |
---|
| Compares two string lexicographically. |
---|
| Compares two string lexicographically, ignoring case considerations. Note: In this case, it will not consider case of a letter (it will ignore whether it is uppercase or lowercase). |
---|
| Converts all the characters in the String to lower case. |
---|
| Converts all the characters in the String to upper case. |
---|
| Returns the copy of the String, by removing whitespaces at both ends. Whitespace characters between words remain unchanged. |
---|
| Generates a new string where every instance of oldChar is substituted with newChar. Note: s1 is still feeksforfeeks and s2 is geeksgorgeeks |
---|
| Returns true if string contains the given string. |
---|
| Converts this String to a new character array. |
---|
| Return true if string starts with this prefix. |
---|
Now, we are going to discuss the working of each method one by one for better understanding.
Implementation of Java String Methods
1. int length() Method
This method provides the total count of characters in the string.
Example:
Java
// Demonstrating the working
// of length() method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.length());
}
}
2. char charAt(int i) Method
This method returns the character at ith index.
Example:
Java
// Demonstrating the working
// of charAt(int i)
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.charAt(7));
}
}
3. String substring(int i) Method
This method return the substring from the ith index character to end.
Example:
Java
// Demonstrating the working
// of substring(int i) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.substring(7));
}
}
4. String substring(int i, int j) Method
This method returns the substring from i to j-1 index.
Example:
Java
// Demonstrating the working
// of substring(int i, int j) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.substring(7, 12));
}
}
5. String concat( String str) Method
This method appends the given string to the end of the current string.
Example:
Java
// Demonstrating the working of concat(String str) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.concat("!!!"));
}
}
6. int indexOf(String s) Method
This method returns the index within the string of the first occurrence of the specified string. If the specified string s is not found in the input string, the method returns -1 by default.
Example:
Java
// Demonstrating the working
// of indexOf(String s) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.indexOf("World"));
}
}
7. int indexOf(String s, int i) Method
This method returns the index within the string of the first occurrence of the specified string, starting at the specified index.
Example:
Java
// Demonstrating the working
// of indexOf(String s, int i) method
public class Geeks {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println(str.indexOf("l", 4));
}
}
8. int lastIndexOf(String s) Method
This method returns the index within the string of the last occurrence of the specified string. If the specified string s is not found in the input string, the method returns -1 by default.
Example:
Java
// Demonstrating the working
// of lastIndexOf(String s) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.lastIndexOf("l"));
}
}
9. boolean equals(Object otherObj) Method
This method compares this string to the specified object.
Example:
Java
// Demonstrating the working
// of equals(Object otherObj) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.equals("Hello, World!"));
}
}
10. boolean equalsIgnoreCase(String anotherString) Method
This method checks if two strings are equal, without considering letter case.
Example:
Java
// Demonstratind the working
// of equalsIgnoreCase(String anotherString) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.equalsIgnoreCase("hello, world!"));
}
}
11. int compareTo(String anotherString) Method
This method compares two string lexicographically.
Example:
Java
// Demonstrating the working of
// compareTo(String anotherString) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.compareTo("Hello, Java!"));
}
}
12. int compareToIgnoreCase(String anotherString) Method
This method compares two string lexicographically, ignoring case considerations.
Example:
Java
// Demonstrating the working of
// compareToIgnoreCase(String anotherString) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.compareToIgnoreCase("hello, java!"));
}
}
13. String toLowerCase() Method
This method converts all the characters in the String to lower case.
Example:
Java
// Demonstrating the working
// of toLowerCase() method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.toLowerCase());
}
}
14. String toUpperCase() Method
This method converts all the characters in the String to upper case.
Example:
Java
// Demonstrating the working
// of toUpperCase() method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.toUpperCase());
}
}
15. String trim() Method
This method returns the copy of the String, by removing whitespaces at both ends. It does not modify the whitespace characters present between the text.
Example:
Java
// Demonstrating the working
// of trim() method
public class Geeks {
public static void main(String[] args) {
String s = " Hello, Trim! ";
System.out.println("'" + s.trim() + "'");
}
}
16. String replace(char oldChar, char newChar) Method
This method returns a new string where all instances of oldChar are replaced by newChar.
Example:
Java
// Demonstrating the working of
// replace(char oldChar, char newChar) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.replace('l', 'x'));
}
}
17. boolean contains(CharSequence sequence) Method
This method returns true if string contains the given string.
Example:
Java
// Demonstrating the working of
// contains(CharSequence sequence) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.contains("World"));
}
}
18. char[] toCharArray() Method
This method converts the string into a new character array.
Example:
Java
// Demonstrating the working
// of toCharArray() method
public class Geeks {
public static void main(String[] args) {
String str = "Hello";
char[] chars = str.toCharArray();
for(char c : chars) {
System.out.print(c + " ");
}
}
}
19. boolean startsWith(String prefix) Method
This method returns true if string starts with this prefix.
Example:
Java
// Demonstrating the working of
// startsWith(String prefix) method
public class Geeks {
public static void main(String[] args) {
String s = "Hello, World!";
System.out.println(s.startsWith("Hello"));
}
}
Similar Reads
Java String valueOf() Method
The valueOf() method of the String class in Java helps to convert various data types like integers, floats, booleans, and objects into their string representations. It makes it simple to work with string manipulation, logging, and displaying data efficiently.Example:To convert a number into text for
3 min read
Java Methods
Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea
8 min read
Object toString() Method in Java
Object class is present in java.lang package. Every class in Java is directly or indirectly derived from the Object class, henceforth, it is a child of the Object class. If a class does not extend any other class then it is a direct child class of Object, and if it extends another class, then it is
3 min read
Method Overloading in Java
In Java, Method Overloading allows us to define multiple methods with the same name but different parameters within a class. This difference may include:The number of parametersThe types of parametersThe order of parametersMethod overloading in Java is also known as Compile-time Polymorphism, Static
10 min read
Java Method References
In Java, a method is a collection of statements that perform some specific task and return the result to the caller. A method reference is the shorthand syntax for a lambda expression that contains just one method call. In general, one does not have to pass arguments to method references.Why Use Met
9 min read
Overriding toString() Method in Java
Java being object-oriented only deals with classes and objects so do if we do require any computation we use the help of object/s corresponding to the class. It is the most frequent method of Java been used to get a string representation of an object. Now you must be wondering that till now they wer
3 min read
run() Method in Java Thread
The run() method is available in the thread class constructed using a separate Runnable object. Otherwise, this method does nothing and returns. We can call the run() method multiple times. The run() method can be called in two ways which are as follows:Using the start() method.Using the run() metho
3 min read
Method within method in java
Java does not support "directly" nested methods. Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile. And in java 8 and newer version you achie
2 min read
Month minus() method in Java
The minus() method is a built-in method of the Month ENUM which is used to get a month before the current month by a specific number of months. That is, this method returns the month before the specified number of months from this month. Syntax: public Month minus(long months) Parameters: This metho
1 min read
Month of() method in Java
The of() method is a built-in method of the Month ENUM which is used to generate a Month instance from an integer value. The integer value should be in the range 1-12 representing any of the 12 months and the method generates a Month instance from it representing a month-of-year. Syntax: public stat
1 min read