String Methods in Java
Introduction :
In Java, strings come with various built-in methods to manipulate and process text efficiently. This guide covers essential string methods, including trim()
, toLowerCase()
, toUpperCase()
, startsWith()
, endsWith()
, replace()
, and replaceFirst()
.
1. String Methods
trim() :
The trim()
method removes all leading and trailing spaces from a string and returns a new trimmed string.
class Main {
public static void main(String[] args) {
String mobile = " 9876543210 ";
System.out.println(mobile.trim());
}
}
Output:
9876543210toLowerCase() :
The toLowerCase()
method converts all characters in a string to lowercase.
class Main {
public static void main(String[] args) {
String name = "HEllO, How ARE yOu?";
System.out.println(name.toLowerCase());
}
}
Output:
toUpperCase() :
The toUpperCase()
method converts all characters in a string to uppercase.
class Main {
public static void main(String[] args) {
String name = "helLO, How aRe yoU?";
System.out.println(name.toUpperCase());
}
}
Output:
startsWith() :
The startsWith()
method checks if a string starts with a specified substring and returns true
or false
.
class Main {
public static void main(String[] args) {
String url = "https://www.google.com/";
System.out.println(url.startsWith("https://"));
}
}
Output:
endsWith() :
The endsWith()
method checks if a string ends with a specified substring and returns true
or false
.
class Main {
public static void main(String[] args) {
String gmailId = "rahul123@gmail.com";
System.out.println(gmailId.endsWith("@gmail.com"));
}
}
Output:
replace() :
The replace()
method replaces all occurrences of a character or substring with a new character or substring.
class Main {
public static void main(String[] args) {
String sentence = "teh cat and teh dog";
System.out.println(sentence.replace("teh", "the"));
}
}
Output:
replaceFirst() :
The replaceFirst()
method replaces only the first occurrence of a substring in a string.
class Main {
public static void main(String[] args) {
String sentence = "teh cat and teh dog";
System.out.println(sentence.replaceFirst("teh", "the"));
}
}
Output:
Handling Characters in replaceFirst() :
The replaceFirst()
method only works with strings, not characters. Attempting to pass a char
will result in a compilation error.
class Main {
public static void main(String[] args) {
String word = "Java";
System.out.println(word.replaceFirst("a", "i"));
}
}
Output:
Summary
- trim() – Removes leading and trailing spaces from a string.
- toLowerCase() – Converts all characters in a string to lowercase.
- toUpperCase() – Converts all characters in a string to uppercase.
- startsWith() – Checks if a string starts with a specific substring and returns
true
orfalse
.
- endsWith() – Checks if a string ends with a specific substring and returns
true
orfalse
.
- replace() – Replaces all occurrences of a specific character or substring with a new value.
- replaceFirst() – Replaces only the first occurrence of a specific substring in a string.
- replaceFirst() limitation – Works only with strings, not with
char
values.
- String methods usage – Useful for text formatting, validation, and manipulation in Java applications.