CodeToConcept
About

java

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

JavaHTMLCSS

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

String Methods in Java

A String method can help answer a small text question: where a word begins, whether two names match, or which part should be displayed. The methods below return results without changing the original String. Pay attention to indexes, whitespace rules, and regular expressions.

Consider this String for the examples:

String text = "Hello Java";
MethodPurpose
length()Returns the number of UTF-16 code units
charAt(index)Returns the character at an index
equals(value)Compares string contents
contains(text)Checks whether text occurs within the string
indexOf(value)Finds the first matching position
substring(start, end)Extracts part of a string
replace(old, new)Replaces matching characters or text
split(regex)Splits a string into an array
trim() / strip()Removes surrounding whitespace
toUpperCase() / toLowerCase()Changes letter case in the returned string

1. length()

The length() method returns the number of UTF-16 code units in the String.

String text = "Hello Java";
 
System.out.println(text.length());

Output

10

2. charAt()

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

Indexes start from 0.

String text = "Java";
 
System.out.println(text.charAt(0));
System.out.println(text.charAt(2));

Output

J
v

Index positions:

J   a   v   a
0   1   2   3

3. equals()

The equals() method compares the contents of two strings.

String a = "Java";
String b = "Java";
 
System.out.println(a.equals(b));

Output

true

4. equalsIgnoreCase()

Compares strings without considering uppercase and lowercase differences.

String a = "Java";
String b = "JAVA";
 
System.out.println(a.equalsIgnoreCase(b));

Output

true

5. == Operator

The == operator compares references when used with objects.

String a = new String("Java");
String b = new String("Java");
 
System.out.println(a == b);

Output

false

Even though both strings contain "Java", they are separate objects.

To compare String contents, use:

a.equals(b);

6. toUpperCase()

Converts the String to uppercase.

String text = "Hello Java";
 
System.out.println(text.toUpperCase());

Output

HELLO JAVA

Remember that the original String is not modified because Strings are immutable.

7. toLowerCase()

Converts the String to lowercase.

String text = "Hello Java";
 
System.out.println(text.toLowerCase());

Output

hello java

8. contains()

Checks whether a String contains a particular sequence of characters.

String text = "Hello Java";
 
System.out.println(text.contains("Java"));

Output

true

9. startsWith()

Checks whether a String starts with a specified prefix.

String text = "Hello Java";
 
System.out.println(text.startsWith("Hello"));

Output

true

10. endsWith()

Checks whether a String ends with a specified suffix.

String text = "Hello Java";
 
System.out.println(text.endsWith("Java"));

Output

true

11. indexOf()

Returns the index of the first occurrence of the specified character or String.

String text = "Hello Java";
 
System.out.println(text.indexOf("Java"));

Output

6

If the value is not found, indexOf() returns -1.

12. lastIndexOf()

Returns the index of the last occurrence.

String text = "Java Java";
 
System.out.println(text.lastIndexOf("Java"));

Output

5

13. substring()

Extracts part of a String.

String text = "Hello Java";
 
System.out.println(text.substring(6));

Output

Java

We can also specify an ending index:

System.out.println(text.substring(0, 5));

Output

Hello

The ending index is excluded.

14. concat()

Combines two strings.

String first = "Hello";
String second = " Java";
 
String result = first.concat(second);
 
System.out.println(result);

Output

Hello Java

15. replace()

Replaces characters or sequences of characters.

String text = "Hello Java";
 
String result = text.replace("Java", "World");
 
System.out.println(result);

Output

Hello World

16. trim()

Removes leading and trailing characters whose value is <= U+0020.

String text = "  Hello Java  ";
 
System.out.println(text.trim());

Output

Hello Java

For broader Unicode whitespace handling, Java also provides strip(), stripLeading(), and stripTrailing().

17. strip()

Since Java 11, strip() removes leading and trailing whitespace as defined by Character.isWhitespace(). It does not remove every Unicode space character, such as a non-breaking space.

String text = "  Hello Java  ";
 
System.out.println(text.strip());

18. isEmpty()

Checks whether the String has zero length.

String text = "";
 
System.out.println(text.isEmpty());

Output

true

19. isBlank()

Available since Java 11, isBlank() checks whether the String is empty or contains only code points recognized by Character.isWhitespace().

String text = "   ";
 
System.out.println(text.isBlank());

Output

true

20. split()

Splits a String based on a regular expression.

String text = "Java,Python,SQL";
 
String[] languages = text.split(",");
 
for (String language : languages) {
    System.out.println(language);
}

Output

Java
Python
SQL

21. toCharArray()

Converts a String into a character array.

String text = "Java";
 
char[] characters = text.toCharArray();
 
for (char character : characters) {
    System.out.println(character);
}

Output

J
a
v
a

22. join()

String.join() joins multiple strings using a delimiter.

String result = String.join(", ", "Java", "Python", "SQL");
 
System.out.println(result);

Output

Java, Python, SQL

23. valueOf()

The valueOf() method converts different types of values into their String representation.

int number = 100;
 
String text = String.valueOf(number);
 
System.out.println(text);

Output

100

Important Point: String Methods Do Not Modify the Original String

Because String objects are immutable:

String text = "java";
 
text.toUpperCase();
 
System.out.println(text);

Output

java

To keep the result, assign the returned string to a variable:

String text = "java";
text = text.toUpperCase();
System.out.println(text);

Output

JAVA

Key Takeaway

The String class provides a wide range of methods for comparing, searching, extracting, transforming, and validating text. Since String objects are immutable, methods that appear to modify a String generally return a new String rather than changing the original object.

See you in next page....🙂

← StringBuilderImmutable vs Mutable Strings →
← StringBuilderImmutable vs Mutable Strings →