Java Inbuilt String Methods - Explanation & Examples
charAt(int index)
Description: Returns the char value at the specified index.
Example: Example: "hello".charAt(1) returns 'e'
codePointAt(int index)
Description: Returns the Unicode code point at the specified index.
Example: Example: "abc".codePointAt(0) returns 97
codePointBefore(int index)
Description: Returns the Unicode code point before the specified index.
Example: Example: "abc".codePointBefore(1) returns 97
codePointCount(int beginIndex, int endIndex)
Description: Returns the number of Unicode code points in the specified text range.
Example: Example: "abc".codePointCount(0, 3) returns 3
compareTo(String anotherString)
Description: Compares two strings lexicographically.
Example: Example: "abc".compareTo("abd") returns -1
compareToIgnoreCase(String str)
Description: Compares two strings lexicographically, ignoring case differences.
Example: Example: "abc".compareToIgnoreCase("ABC") returns 0
concat(String str)
Description: Concatenates the specified string to the end of this string.
Example: Example: "hello".concat("world") returns "helloworld"
contains(CharSequence s)
Description: Returns true if and only if this string contains the specified sequence of char values.
Example: Example: "hello".contains("ell") returns true
contentEquals(CharSequence cs)
Description: Compares this string to the specified CharSequence.
Example: Example: "test".contentEquals("test") returns true
endsWith(String suffix)
Description: Tests if this string ends with the specified suffix.
Example: Example: "hello".endsWith("lo") returns true
Java Inbuilt String Methods - Explanation & Examples
equals(Object anObject)
Description: Compares this string to the specified object.
Example: Example: "hello".equals("hello") returns true
equalsIgnoreCase(String anotherString)
Description: Compares this String to another String, ignoring case considerations.
Example: Example: "HELLO".equalsIgnoreCase("hello") returns true
format(String format, Object... args)
Description: Returns a formatted string using the specified format string and arguments.
Example: Example: String.format("Name: %s", "John") returns "Name: John"
getBytes()
Description: Encodes this String into a sequence of bytes using the platform's default charset.
Example: Example: "abc".getBytes() returns byte array [97, 98, 99]
getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Description: Copies characters from this string into the destination character array.
Example: Example: "hello".getChars(0, 2, dstArray, 0)
indexOf(int ch)
Description: Returns the index of the first occurrence of the specified character.
Example: Example: "hello".indexOf('e') returns 1
indexOf(String str)
Description: Returns the index of the first occurrence of the specified substring.
Example: Example: "hello".indexOf("ll") returns 2
indexOf(int ch, int fromIndex)
Description: Returns the index of the first occurrence of the character from a given index.
Example: Example: "hello".indexOf('l', 3) returns 3
indexOf(String str, int fromIndex)
Description: Returns the index of the first occurrence of the substring from a given index.
Example: Example: "hellohello".indexOf("lo", 5) returns 8
intern()
Description: Returns a canonical representation for the string object.
Example: Example: "abc".intern() returns reference from string pool
Java Inbuilt String Methods - Explanation & Examples
isEmpty()
Description: Returns true if the length of the string is 0.
Example: Example: "".isEmpty() returns true
lastIndexOf(int ch)
Description: Returns the index of the last occurrence of the specified character.
Example: Example: "hello".lastIndexOf('l') returns 3
lastIndexOf(String str)
Description: Returns the index of the last occurrence of the specified substring.
Example: Example: "hellohello".lastIndexOf("lo") returns 8
length()
Description: Returns the length of the string.
Example: Example: "hello".length() returns 5
matches(String regex)
Description: Tells whether or not this string matches the given regular expression.
Example: Example: "abc".matches("[a-z]+") returns true
replace(char oldChar, char newChar)
Description: Returns a string resulting from replacing all occurrences of oldChar with newChar.
Example: Example: "hello".replace('l', 'p') returns "heppo"
replace(CharSequence target, CharSequence replacement)
Description: Replaces each substring matching target with replacement.
Example: Example: "hello".replace("ll", "rr") returns "herro"
replaceAll(String regex, String replacement)
Description: Replaces each substring matching the regex with the replacement.
Example: Example: "abc123".replaceAll("\\d", "") returns "abc"
replaceFirst(String regex, String replacement)
Description: Replaces the first substring matching the regex with the replacement.
Example: Example: "abc123abc".replaceFirst("abc", "x") returns "x123abc"
split(String regex)
Description: Splits this string around matches of the given regex.
Example: Example: "a,b,c".split(",") returns ["a", "b", "c"]
Java Inbuilt String Methods - Explanation & Examples
split(String regex, int limit)
Description: Splits this string into a maximum number of substrings.
Example: Example: "a,b,c".split(",", 2) returns ["a", "b,c"]
startsWith(String prefix)
Description: Tests if this string starts with the specified prefix.
Example: Example: "hello".startsWith("he") returns true
startsWith(String prefix, int toffset)
Description: Tests if the substring starting at the specified index starts with the specified prefix.
Example: Example: "hello".startsWith("ll", 2) returns true
subSequence(int beginIndex, int endIndex)
Description: Returns a new character sequence from beginIndex to endIndex.
Example: Example: "hello".subSequence(1, 3) returns "el"
substring(int beginIndex)
Description: Returns a substring from the specified index to the end.
Example: Example: "hello".substring(2) returns "llo"
substring(int beginIndex, int endIndex)
Description: Returns a substring between the specified indexes.
Example: Example: "hello".substring(1, 4) returns "ell"
toCharArray()
Description: Converts the string to a new character array.
Example: Example: "abc".toCharArray() returns ['a', 'b', 'c']
toLowerCase()
Description: Converts all characters in the string to lower case.
Example: Example: "HELLO".toLowerCase() returns "hello"
toUpperCase()
Description: Converts all characters in the string to upper case.
Example: Example: "hello".toUpperCase() returns "HELLO"
trim()
Description: Returns a copy of the string with leading and trailing whitespace removed.
Example: Example: " hello ".trim() returns "hello"
Java Inbuilt String Methods - Explanation & Examples
valueOf(...)
Description: Returns the string representation of the argument.
Example: Example: String.valueOf(10) returns "10"