String Methods in Java
A String method can help answer a small text question: where a word begins, whether two names match, or which part should be displayed. The methods below return results without changing the original String. Pay attention to indexes, whitespace rules, and regular expressions.
Consider this String for the examples:
String text = "Hello Java";| Method | Purpose |
|---|---|
length() | Returns the number of UTF-16 code units |
charAt(index) | Returns the character at an index |
equals(value) | Compares string contents |
contains(text) | Checks whether text occurs within the string |
indexOf(value) | Finds the first matching position |
substring(start, end) | Extracts part of a string |
replace(old, new) | Replaces matching characters or text |
split(regex) | Splits a string into an array |
trim() / strip() | Removes surrounding whitespace |
toUpperCase() / toLowerCase() | Changes letter case in the returned string |
1. length()
The length() method returns the number of UTF-16 code units in the String.
String text = "Hello Java";
System.out.println(text.length());Output
102. charAt()
The charAt() method returns the character at a specified index.
Indexes start from 0.
String text = "Java";
System.out.println(text.charAt(0));
System.out.println(text.charAt(2));Output
J
vIndex positions:
J a v a
0 1 2 33. equals()
The equals() method compares the contents of two strings.
String a = "Java";
String b = "Java";
System.out.println(a.equals(b));Output
true4. equalsIgnoreCase()
Compares strings without considering uppercase and lowercase differences.
String a = "Java";
String b = "JAVA";
System.out.println(a.equalsIgnoreCase(b));Output
true5. == Operator
The == operator compares references when used with objects.
String a = new String("Java");
String b = new String("Java");
System.out.println(a == b);Output
falseEven though both strings contain "Java", they are separate objects.
To compare String contents, use:
a.equals(b);6. toUpperCase()
Converts the String to uppercase.
String text = "Hello Java";
System.out.println(text.toUpperCase());Output
HELLO JAVARemember that the original String is not modified because Strings are immutable.
7. toLowerCase()
Converts the String to lowercase.
String text = "Hello Java";
System.out.println(text.toLowerCase());Output
hello java8. contains()
Checks whether a String contains a particular sequence of characters.
String text = "Hello Java";
System.out.println(text.contains("Java"));Output
true9. startsWith()
Checks whether a String starts with a specified prefix.
String text = "Hello Java";
System.out.println(text.startsWith("Hello"));Output
true10. endsWith()
Checks whether a String ends with a specified suffix.
String text = "Hello Java";
System.out.println(text.endsWith("Java"));Output
true11. indexOf()
Returns the index of the first occurrence of the specified character or String.
String text = "Hello Java";
System.out.println(text.indexOf("Java"));Output
6If the value is not found, indexOf() returns -1.
12. lastIndexOf()
Returns the index of the last occurrence.
String text = "Java Java";
System.out.println(text.lastIndexOf("Java"));Output
513. substring()
Extracts part of a String.
String text = "Hello Java";
System.out.println(text.substring(6));Output
JavaWe can also specify an ending index:
System.out.println(text.substring(0, 5));Output
HelloThe ending index is excluded.
14. concat()
Combines two strings.
String first = "Hello";
String second = " Java";
String result = first.concat(second);
System.out.println(result);Output
Hello Java15. replace()
Replaces characters or sequences of characters.
String text = "Hello Java";
String result = text.replace("Java", "World");
System.out.println(result);Output
Hello World16. trim()
Removes leading and trailing characters whose value is <= U+0020.
String text = " Hello Java ";
System.out.println(text.trim());Output
Hello JavaFor broader Unicode whitespace handling, Java also provides strip(), stripLeading(), and stripTrailing().
17. strip()
Since Java 11, strip() removes leading and trailing whitespace as defined by Character.isWhitespace(). It does not remove every Unicode space character, such as a non-breaking space.
String text = " Hello Java ";
System.out.println(text.strip());18. isEmpty()
Checks whether the String has zero length.
String text = "";
System.out.println(text.isEmpty());Output
true19. isBlank()
Available since Java 11, isBlank() checks whether the String is empty or contains only code points recognized by Character.isWhitespace().
String text = " ";
System.out.println(text.isBlank());Output
true20. split()
Splits a String based on a regular expression.
String text = "Java,Python,SQL";
String[] languages = text.split(",");
for (String language : languages) {
System.out.println(language);
}Output
Java
Python
SQL21. toCharArray()
Converts a String into a character array.
String text = "Java";
char[] characters = text.toCharArray();
for (char character : characters) {
System.out.println(character);
}Output
J
a
v
a22. join()
String.join() joins multiple strings using a delimiter.
String result = String.join(", ", "Java", "Python", "SQL");
System.out.println(result);Output
Java, Python, SQL23. valueOf()
The valueOf() method converts different types of values into their String representation.
int number = 100;
String text = String.valueOf(number);
System.out.println(text);Output
100Important Point: String Methods Do Not Modify the Original String
Because String objects are immutable:
String text = "java";
text.toUpperCase();
System.out.println(text);Output
javaTo keep the result, assign the returned string to a variable:
String text = "java";
text = text.toUpperCase();
System.out.println(text);Output
JAVAKey Takeaway
The String class provides a wide range of methods for comparing, searching, extracting, transforming, and validating text. Since String objects are immutable, methods that appear to modify a String generally return a new String rather than changing the original object.
See you in next page....🙂