Java String Methods (Advanced)
Java String Methods (Advanced)
Java provides many built-in methods to work with strings. These methods allow searching, modifying, comparing, trimming, splitting, and formatting string values.
Below are the most commonly used advanced string methods with examples.
🔹 1. charAt()
Returns a character at a specific index.
🔹 2. substring()
Extracts a part of the string.
🔹 3. contains()
Checks whether a string contains a sequence of characters.
🔹 4. startsWith() and endsWith()
🔹 5. replace() and replaceAll()
replaceAll() is used for regex patterns:
🔹 6. trim()
Removes leading and trailing spaces.
🔹 7. split()
Splits the string into an array.
🔹 8. toCharArray()
Converts string into an array of characters.
🔹 9. compareTo()
Compares two strings lexicographically.
🔹 10. format()
Formats the string like printf.
🔹 11. repeat() (Java 11+)
Repeats a string multiple times.
🔹 12. isEmpty() and isBlank() (Java 11+)
⭐ Full Example Using Multiple Methods
📌 Summary
| Method | Purpose |
|---|---|
charAt(), toCharArray() |
Character access |
substring() |
Extract part of string |
replace(), replaceAll() |
Modify text |
contains(), startsWith(), endsWith() |
Search in strings |
trim(), isEmpty(), isBlank() |
Clean text |
split() |
Break string |
compareTo() |
Compare strings |
format(), repeat() |
Formatting & repetition |
