Java String Methods
Java provides a wide range of methods to manipulate and handle strings effectively. These methods are part of the String class, which is included in the java.lang package.
Key Topics
- length()
- charAt(int index)
- substring(int beginIndex, int endIndex)
- toLowerCase()
- toUpperCase()
- trim()
- replace(char oldChar, char newChar)
- indexOf()
- contains()
- startsWith()
- endsWith()
- split()
- concat()
1. length()
Returns the length of the string.
Example
public class StringLengthExample {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println("Length: " + str.length());
}
}
Output:
2. charAt(int index)
Returns the character at the specified index.
Example
public class StringCharAtExample {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println("Character at index 1: " + str.charAt(1));
}
}
Output:
3. substring(int beginIndex, int endIndex)
Returns a substring from the specified begin index to the end index.
Example
public class StringSubstringExample {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println("Substring (7, 12): " + str.substring(7, 12));
}
}
Output:
4. toLowerCase()
Converts all characters in the string to lowercase.
Example
public class StringToLowerCaseExample {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println("Lowercase: " + str.toLowerCase());
}
}
Output:
5. toUpperCase()
Converts all characters in the string to uppercase.
Example
public class StringToUpperCaseExample {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println("Uppercase: " + str.toUpperCase());
}
}
Output:
6. trim()
Removes leading and trailing whitespace.
Example
public class StringTrimExample {
public static void main(String[] args) {
String str = " Hello, World! ";
System.out.println("Trimmed: " + str.trim());
}
}
Output:
7. replace(char oldChar, char newChar)
Replaces all occurrences of the specified old character with the new character.
Example
public class StringReplaceExample {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println("Replaced: " + str.replace('o', '0'));
}
}
Output:
8. indexOf()
Returns the index within this string of the first occurrence of the specified character or substring.
Example
public class StringIndexOfExample {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println("Index of 'W': " + str.indexOf('W'));
System.out.println("Index of 'World': " + str.indexOf("World"));
}
}
Output:
Index of 'World': 7
9. contains()
Checks if the string contains the specified character sequence.
Example
public class StringContainsExample {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println("Contains 'World': " + str.contains("World"));
System.out.println("Contains 'Java': " + str.contains("Java"));
}
}
Output:
Contains 'Java': false
10. startsWith()
Checks if the string starts with the specified prefix.
Example
public class StringStartsWithExample {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println("Starts with 'Hello': " + str.startsWith("Hello"));
System.out.println("Starts with 'World': " + str.startsWith("World"));
}
}
Output:
Starts with 'World': false
11. endsWith()
Checks if the string ends with the specified suffix.
Example
public class StringEndsWithExample {
public static void main(String[] args) {
String str = "Hello, World!";
System.out.println("Ends with 'World!': " + str.endsWith("World!"));
System.out.println("Ends with 'Hello': " + str.endsWith("Hello"));
}
}
Output:
Ends with 'Hello': false
12. split()
Splits the string into an array of substrings based on the specified delimiter.
Example
public class StringSplitExample {
public static void main(String[] args) {
String str = "Hello, World! Welcome to Java.";
String[] words = str.split(" ");
for (String word : words) {
System.out.println(word);
}
}
}
Output:
World!
Welcome
to
Java.
13. concat()
Concatenates the specified string to the end of the current string.
Example
public class StringConcatExample {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = " World";
System.out.println("Concatenated String: " + str1.concat(str2));
}
}
Output:
Key Takeaways
- Java String methods provide a powerful way to manipulate and handle strings.
- Methods like length(), charAt(), substring(), toLowerCase(), toUpperCase(), trim(), replace(), indexOf(), contains(), startsWith(), endsWith(), split(), and concat() are commonly used for various string operations.
- Understanding and using these methods effectively can enhance your Java programming skills.