Java String Methods
The String class has many methods. These are the ones you will use most. All the examples use this text.
Basic methods
public class Main {
public static void main(String[] args) {
String s = "Learn Java Step by Step";
System.out.println(s.length()); // number of characters
System.out.println(s.charAt(0)); // first character
System.out.println(s.toUpperCase());
System.out.println(s.toLowerCase());
System.out.println(s.isEmpty());
System.out.println(s.contains("Java"));
System.out.println(s.startsWith("Learn"));
System.out.println(s.endsWith("Step"));
}
}
Output
23
L
LEARN JAVA STEP BY STEP
learn java step by step
false
true
true
true
Find and cut
public class Find {
public static void main(String[] args) {
String s = "Learn Java Step by Step";
System.out.println(s.indexOf("Java")); // first position
System.out.println(s.indexOf("Step"));
System.out.println(s.lastIndexOf("Step")); // last position
System.out.println(s.indexOf("Python")); // -1 if not found
System.out.println(s.substring(6)); // from index 6
System.out.println(s.substring(6, 10)); // from 6 to before 10
}
}
Output
6
11
19
-1
Java Step by Step
Java
The positions start at 0. For substring(start, end) the end index is not included.
Change text
public class Change {
public static void main(String[] args) {
String s = "I like Java. Java is fun.";
System.out.println(s.replace("Java", "Python"));
System.out.println(s.replaceFirst("Java", "C"));
System.out.println(" hello ".trim() + "|");
System.out.println(" hello ".strip() + "|");
System.out.println("ab".repeat(3));
}
}
Output
I like Python. Python is fun.
I like C. Java is fun.
hello|
hello|
ababab
Split and join
public class Split {
public static void main(String[] args) {
String csv = "red,green,blue";
String[] colors = csv.split(",");
System.out.println(colors.length);
for (String c : colors) {
System.out.println(c);
}
System.out.println(String.join(" | ", colors));
}
}
Output
3
red
green
blue
red | green | blue
Format text
public class Format {
public static void main(String[] args) {
String name = "Riya";
int age = 21;
double marks = 87.456;
System.out.println(String.format("%s is %d years old", name, age));
System.out.printf("Marks: %.2f%n", marks); // 2 decimal places
System.out.println("%s scored %.1f".formatted(name, marks));
}
}
Output
Riya is 21 years old
Marks: 87.46
Riya scored 87.5
| Format | Meaning |
|---|---|
| %s | String |
| %d | Whole number |
| %f, %.2f | Decimal number (with 2 digits after the point) |
| %n | New line (works on every system) |
| %c | Character |
| %b | Boolean |
Check the characters
public class Chars {
public static void main(String[] args) {
String s = "Java25";
int letters = 0, digits = 0;
for (char c : s.toCharArray()) {
if (Character.isLetter(c)) letters++;
if (Character.isDigit(c)) digits++;
}
System.out.println("Letters: " + letters + ", digits: " + digits);
System.out.println(new StringBuilder("stressed").reverse());
}
}
Output
Letters: 4, digits: 2
desserts
Quick list
| Method | Use |
|---|---|
| length() | Number of characters |
| charAt(i) | Character at a position |
| substring(a, b) | Part of the text |
| indexOf(x) | Position of the first x, -1 when missing |
| equals(x), equalsIgnoreCase(x) | Compare the text |
| contains(x), startsWith(x), endsWith(x) | Check the text |
| replace(a, b) | Replace text |
| split(x) | Break into an array |
| trim(), strip() | Remove the spaces at both ends |
| toUpperCase(), toLowerCase() | Change the case |
| isEmpty(), isBlank() | Empty check |
| repeat(n) | Repeat the text |
Note:
All these methods return a new string. The original string never changes. Next: Java StringBuilder and Text Blocks.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |