![]() |
VOOZH | about |
The String class in Java provides a rich set of methods for performing string manipulation, comparison, searching, formatting, and Unicode handling. Since strings are immutable, these methods return new values without modifying the original string object.
1. codePointAt(int index): It returns the Unicode code point of the character at the specified index.
Syntax:
int codePointAt(int index)
Example:
int cp = "Java".codePointAt(1);
System.out.println(cp);
2. codePointBefore(int index): Returns the Unicode code point of the character before the specified index.
Syntax:
int codePointBefore(int index)
Example:
int cp = "Java".codePointBefore(2);
System.out.println(cp);
3. codePointCount(int beginIndex, int endIndex): Returns the number of Unicode code points in the specified range.
Syntax:
int codePointCount(int beginIndex, int endIndex)
Example:
int count = "Hello".codePointCount(0, 5);
System.out.println(count);
4.subSequence(int beginIndex, int endIndex): Returns a CharSequence that is a subsequence of the string.
Syntax:
CharSequence subSequence(int beginIndex, int endIndex)
Example:
CharSequence cs = "Programming".subSequence(0, 7);
System.out.println(cs);
5. contains(CharSequence s): Checks if the string contains the specified sequence.
Syntax:
boolean contains(CharSequence s)
Example:
boolean result = "Java Programming".contains("Program");
System.out.println(result);
6. contentEquals(CharSequence s): Checks if the given CharSequence exactly matches the string.
Syntax:
boolean contentEquals(CharSequence s)
Example:
boolean result =
"Java".contentEquals(new StringBuilder("Java"));
System.out.println(result);
7. endsWith(String suffix): Returns true if the string ends with the specified suffix.
Syntax:
boolean endsWith(String suffix)
Example:
boolean result = "file.txt".endsWith(".txt");
System.out.println(result);
8. startsWith(String prefix): Returns true if the string starts with the specified prefix.
Syntax:
boolean startsWith(String prefix)
Example:
boolean result = "https://example.com".startsWith("https");
System.out.println(result);
9. getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin): Copies characters from a string into a destination character array.
Syntax:
void getChars(int srcBegin,
int srcEnd,
char[] dst,
int dstBegin)
Example:
char[] arr = new char[4];
"Java".getChars(0, 4, arr, 0);
System.out.println(arr);
10. toCharArray(): Converts the entire string into a new character array.
Syntax:
char[] toCharArray()
Example:
char[] chars = "Java".toCharArray();
Note: Use getChars() when copying partial content into an existing array.
11. hashCode(): Returns the hash code for the string using the formula:
Syntax:
int hashCode()
Example:
int hash = "Java".hashCode();
System.out.println(hash);
12. intern(): Returns the canonical representation of the string from the String pool.
Syntax:
String intern()
Example:
String s1 = new String("Java").intern();
13. isEmpty(): Returns true if the string length is 0.
Syntax:
boolean isEmpty()
Example:
boolean result = "".isEmpty();
System.out.println(result);
14. format(String format, Object... args): Returns a formatted string using format specifiers.
Syntax:
static String format(String format, Object... args)
Example:
String s = String.format("Age: %d", 25);
System.out.println(s);
15. matches(String regex): Checks if the string matches the given regular expression.
Syntax:
boolean matches(String regex)
Example:
boolean valid = "abc123".matches("[a-z]+\\d+");
System.out.println(valid);
16. regionMatches(...): Compares a region of one string with a region of another string.
Syntax:
boolean regionMatches(int toffset,
String other,
int ooffset,
int len)
Example:
boolean result = "HelloWorld".regionMatches( 5, "World", 0, 5);
System.out.println(result);
17. split(String regex): Splits the string using a regular expression.
Syntax:
String[] split(String regex)
Example:
String[] parts = "A,B,C".split(",");
18. join(CharSequence delimiter, CharSequence... elements): Joins elements using the specified delimiter.
Syntax:
static String join(CharSequence delimiter, CharSequence... elements)
Example:
String s =String.join("-", "2025", "04", "30");
System.out.println(s);
19. replaceAll(String regex, String replacement): Replaces all occurrences matching the regex.
Syntax:
String replaceAll(String regex, String replacement)
Example:
String s ="a1b2".replaceAll("\\d", "");
System.out.println(s);
20. replaceFirst(String regex, String replacement): Replaces only the first matching occurrence.
Syntax:
String replaceFirst(String regex, String replacement)
Example:
String s = "a1b2".replaceFirst("\\d", "");
System.out.println(s);