Java String Methods
Java string methods are built-in actions you can call on a String value. They help you inspect text, search inside it, extract pieces, compare it, and create changed versions.
You call a string method with dot syntax, such as message.length() or name.toUpperCase().
Inspecting A String
Use length() to count characters and charAt() to read one character at a specific index. String indexes start at 0, so the first character is at index 0.
public class Main {
public static void main(String[] args) {
String word = "Library";
System.out.println("Text: " + word);
System.out.println("Length: " + word.length());
System.out.println("First: " + word.charAt(0));
System.out.println("Last: " + word.charAt(word.length() - 1));
}
}
Output:
Text: Library
Length: 7
First: L
Last: y
The expression word.length() - 1 gives the last valid index. For "Library", the length is 7, so the last index is 6.
Searching Inside Text
Use contains() when you only need a true or false answer. Use indexOf() when you need to know where the text starts.
| Method | What it returns |
|---|---|
contains(text) |
true if the string contains the text, otherwise false. |
indexOf(text) |
The first matching index, or -1 if the text is not found. |
startsWith(text) |
true if the string begins with the text. |
endsWith(text) |
true if the string ends with the text. |
public class Main {
public static void main(String[] args) {
String fileName = "report-final.pdf";
System.out.println(fileName.contains("final"));
System.out.println(fileName.indexOf("final"));
System.out.println(fileName.startsWith("report"));
System.out.println(fileName.endsWith(".pdf"));
System.out.println(fileName.indexOf("draft"));
}
}
Output:
true
7
true
true
-1
These searches are case-sensitive. For example, contains("Final") would be false for "report-final.pdf".
Extracting Part Of A String
The substring() method returns part of a string. With two indexes, substring(start, end) starts at start and stops before end.
public class Main {
public static void main(String[] args) {
String code = "JAVA-2026-BASICS";
String topic = code.substring(0, 4);
String year = code.substring(5, 9);
String level = code.substring(10);
System.out.println("Topic: " + topic);
System.out.println("Year: " + year);
System.out.println("Level: " + level);
}
}
Output:
Topic: JAVA
Year: 2026
Level: BASICS
The one-argument version, such as substring(10), returns everything from that index to the end of the string.
Creating Changed Versions
Strings in Java are immutable, which means a method does not edit the original string. Methods such as trim(), replace(), toLowerCase(), and toUpperCase() return new strings.
public class Main {
public static void main(String[] args) {
String input = " Java Basics ";
String cleaned = input.trim();
String lower = cleaned.toLowerCase();
String updated = lower.replace("basics", "methods");
System.out.println("Original: [" + input + "]");
System.out.println("Cleaned: [" + cleaned + "]");
System.out.println("Updated: " + updated);
}
}
Output:
Original: [ Java Basics ]
Cleaned: [Java Basics]
Updated: java methods
Comparing Text
Use equals() to compare strings with matching case. Use equalsIgnoreCase() when uppercase and lowercase differences should not matter.
public class Main {
public static void main(String[] args) {
String savedAnswer = "java";
String userAnswer = "Java";
System.out.println(savedAnswer.equals(userAnswer));
System.out.println(savedAnswer.equalsIgnoreCase(userAnswer));
}
}
Output:
false
true
Common Mistakes
- Do not use an index less than
0or greater than the last valid index. - Remember that
substring(start, end)stops beforeend. - Assign the return value when you want to keep a changed version of a string.
- Use
equals()orequalsIgnoreCase()for text comparison, not==.
Takeaway: Java string methods let you read, search, extract, compare, and transform text while keeping the original string unchanged.
