DEV Community

Abishek
Abishek

Posted on

Mastering Java String Methods: Top 20 Methods with Examples

Introduction

Strings are a fundamental part of Java programming. To work efficiently with text, developers must understand the built-in methods provided by the String class.

In this blog, we will explore 20 important Java String methods with clear examples.

What is a String in Java?

  • In Java, a String is the type of object that can store a sequence of characters enclosed by double quotes and every character is stored in 16 bits, i.e., using UTF 16-bit encoding. A string acts the same as an array of characters. Java provides a robust and flexible API for handling strings, allowing for various operations such as concatenation, comparison and manipulation.--->(TBD)

how to create String?

String name = "Abishek";
Enter fullscreen mode Exit fullscreen mode
String name = new String("Abishek")
Enter fullscreen mode Exit fullscreen mode

String Methods

1.length()

"Java String".length(); // 11
Enter fullscreen mode Exit fullscreen mode

What it returns:
It returns an integer showing the total number of characters in the string.

Why we need it:
We use it when we want to know how long a string is—for example, validating input length or looping through characters.

How it works:
Java counts each character in the string, including spaces, and returns the total count

2. charAt()

"Java String".charAt(5);
Enter fullscreen mode Exit fullscreen mode

What it returns:
It returns a single character at the specified index.

Why we need it:
Useful when working with individual characters, such as checking letters or building custom logic.

How it works:
Java treats the string as a sequence of characters and retrieves the one at the given position (index starts from 0).

3. indexOf()

"Java String".indexOf("a"); // 1
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns the index of the first occurrence of the given character. If not found, it returns -1.

Why we need it:
Helpful when searching for specific characters or words inside a string.

How it works:
Java scans the string from left to right and stops at the first match.

4. lastIndexOf()

"Java String".lastIndexOf("a"); // 3
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns the index of the last occurrence of the character.

Why we need it:
Useful when a character appears multiple times and you need the final position.

How it works:
Java searches the string from right to left to find the last match.

5. isEmpty()

"".isEmpty(); // true
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns true if the string is empty, otherwise false.

Why we need it:
Commonly used for input validation to check if the user entered anything.

How it works:
It checks whether the string length is zero

6. equals()

"Java String".equals("Java String"); // true
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns true if both strings are exactly equal.

Why we need it:
Used to compare string values correctly (instead of using ==).

How it works:
Java compares each character of both strings one by one.

7. equalsIgnoreCase()

"java string".equalsIgnoreCase("JAVA STRING"); // true
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns true if both strings are equal, ignoring case differences.

Why we need it:
Useful when user input may vary in uppercase/lowercase.

How it works:
Java converts both strings to the same case before comparing.

8. compareTo()

"Java".compareTo("String");
Enter fullscreen mode Exit fullscreen mode

What it returns:

0 → equal
negative → comes before
positive → comes after

Why we need it:
Used for sorting strings in alphabetical order.

How it works:
Java compares characters based on Unicode values.

9. compareToIgnoreCase()

"java".compareToIgnoreCase("JAVA"); // 0
Enter fullscreen mode Exit fullscreen mode

What it returns:
Same as compareTo() but ignores case.

Why we need it:
Useful for sorting or comparing without worrying about uppercase/lowercase.

How it works:
Internally converts both strings to the same case before comparing.

10. contains()

"Java String".contains("String"); // true
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns true if the substring exists.

Why we need it:
Helpful for checking if a word or phrase is present.

How it works:
Java checks if the given sequence appears anywhere inside the string.

11. substring()

"Java String".substring(5, 11); // String
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns a part of the original string.

Why we need it:
Useful for extracting specific portions of data.

How it works:
It takes characters from start index up to (but not including) end index.

12. replace()

"Java String".replace("Java", "C++"); // C++ String
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns a new string with replaced values.

Why we need it:
Used when modifying text, such as replacing words or characters.

How it works:
Java replaces all matching parts and creates a new string.

13. replaceAll()

"Java123 String".replaceAll("\\d", ""); // Java String
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns a new string after replacing patterns.

Why we need it:
Useful for removing numbers, spaces, or special patterns.

How it works:
Uses regular expressions (regex) to match and replace content.

14. toUpperCase()

"Java String".toUpperCase(); // JAVA STRING
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns the string in uppercase.

Why we need it:
Useful for formatting or standardizing text.

How it works:
Converts each character to its uppercase equivalent.

15. toLowerCase()

"Java String".toLowerCase(); // java string
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns the string in lowercase.

Why we need it:
Used when case-insensitive comparison is needed.

How it works:
Converts each character to lowercase.

16. trim()

"  Java String  ".trim(); // Java String
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns a string without leading and trailing spaces.

Why we need it:
Useful when cleaning user input.

How it works:
Removes spaces only from the beginning and end.

17. split()

"Java,String".split(","); //[Java,String]
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns an array of strings.

Why we need it:
Useful for breaking data into parts (like CSV values).

How it works:
Splits the string based on a delimiter.

18. concat()

"Java".concat(" String"); // Java String
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns a new combined string.

Why we need it:
Used to join strings together.

How it works:
Adds the second string to the first and returns a new string.

19. join()

String.join(" ", "Java", "String"); // Java String
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns a single string with joined values.

Why we need it:
Useful when combining multiple values with a separator.

How it works:
Adds a delimiter between each element and joins them.

20. matches()

"Java123".matches("[A-Za-z0-9]+"); // true
Enter fullscreen mode Exit fullscreen mode

What it returns:
Returns true if the string matches the pattern.

Why we need it:
Used for validation (email, password, etc.).

How it works:
Checks the string against a regex pattern.--->(TBD)

Top comments (0)