Strings in Java are immutable, meaning any operation that appears to modify a string actually creates a new String object. This article discusses some important constructors and methods provided by the java.lang.String class, especially those related to Unicode handling, comparison, and manipulation.
String Class Methods
1. codePointAt(int index): It returns the Unicode code point of the character at the specified index.
Syntax:
int cp = "Java".codePointAt(1); // 'a'
2. codePointBefore(int index): Returns the Unicode code point of the character before the specified index.
Syntax:
int cp = "Java".codePointBefore(2); // 'a'
3. codePointCount(int beginIndex, int endIndex): Returns the number of Unicode code points in the specified range.
Syntax:
int count = "Hello".codePointCount(0, 5);
4.subSequence(int beginIndex, int endIndex): Returns a CharSequence that is a subsequence of the string.
CharSequence cs = "Programming".subSequence(0, 7);
5. contains(CharSequence s): Checks if the string contains the specified sequence.
boolean result = "Java Programming".contains("Program");
6. contentEquals(CharSequence s): Checks if the given CharSequence exactly matches the string.
boolean result = "Java".contentEquals(new StringBuilder("Java"));
7. endsWith(String suffix): Returns true if the string ends with the specified suffix.
boolean result = "filename.txt".endsWith(".txt");
8. startsWith(String prefix): Returns true if the string starts with the specified prefix.
boolean result = "https://example.com".startsWith("https");
9. getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin): Copies characters from a string into a destination character array.
char[] arr = new char[4];
"Java".getChars(0, 4, arr, 0);
10. toCharArray(): Converts the entire string into a new character array.
char[] chars = "Java".toCharArray();
Note: Use getChars() when copying partial content into an existing array.
10. hashCode(): Returns the hash code for the string using the formula:
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
int hash = "Java".hashCode();
11. intern(): Returns the canonical representation of the string from the String pool.
String s1 = new String("Java").intern();
12. isEmpty(): Returns true if the string length is 0.
boolean result = "".isEmpty();
13. format(String format, Object... args): Returns a formatted string using format specifiers.
String s = String.format("Age: %d", 25);
14. matches(String regex): Checks if the string matches the given regular expression.
boolean valid = "abc123".matches("[a-z]+\\d+");
15. regionMatches(...): Compares a region of one string with a region of another string.
boolean result =
"HelloWorld".regionMatches(5, "World", 0, 5);
16. split(String regex): Splits the string using a regular expression.
String[] parts = "A,B,C".split(",");
17. join(CharSequence delimiter, CharSequence... elements): Joins elements using the specified delimiter.
String s = String.join("-", "2025", "04", "30");
18. replaceAll(String regex, String replacement): Replaces all occurrences matching the regex.
String s = "a1b2".replaceAll("\\d", "");
19.replaceFirst(String regex, String replacement): Replaces only the first matching occurrence.
String s = "a1b2".replaceFirst("\\d", "");