String Methods
String Methods
charAt(int index) Returns the character at the specified index. String str = "Hello"; char result = str.charAt(1); result
will be 'e'
length() Returns the length of the string. String str = "Hello"; int length = str.length(); length will
be 5
concat(String str) Concatenates the specified string to the end of the current String str1 = "Hello"; String str2 = " World"; String result
string. = str1.concat(str2);
equals(Object obj) Compares the content of two strings. String str1 = "Hello"; String str2 = "hello"; boolean
isEqual = str1.equals(str2);
equalsIgnoreCase(String Compares two strings, ignoring case differences. String str1 = "Hello"; String str2 = "hello"; boolean
anotherString) isEqual = str1.equalsIgnoreCase(str2);
indexOf(int ch) Returns the index of the first occurrence of the specified String str = "Hello"; int index = str.indexOf('l'); index
character. will be 2
lastIndexOf(int ch) Returns the index of the last occurrence of the specified String str = "Hello"; int lastIndex = str.lastIndexOf('l');
character. lastIndex will be 3
indexOf(int ch, int fromIndex) Returns the index of the first occurrence of the specified String str = "Hello"; int index = str.indexOf('l', 3); index
character, starting the search at the specified index. will be 3
lastIndexOf(int ch, int fromIndex) Returns the index of the last occurrence of the specified String str = "Hello"; int lastIndex = str.lastIndexOf('l', 2);
character, searching backward from the specified index. lastIndex will be 2
substring(int beginIndex) Returns a new string that is a substring of the original string. String str = "Hello"; String subStr = str.substring(2);
subStr will be "llo"
substring(int beginIndex, int Returns a new string that is a substring of the original string, String str = "Hello"; String subStr = str.substring(2, 4);
endIndex) starting from the specified begin index and ending at the subStr will be "ll"
specified end index (exclusive).
toLowerCase() Converts all characters in the string to lowercase. String str = "Hello"; String lowerCaseStr =
str.toLowerCase(); lowerCaseStr will be "hello"
toUpperCase() Converts all characters in the string to uppercase. String str = "Hello"; String upperCaseStr =
str.toUpperCase(); upperCaseStr will be "HELLO"
trim() Removes leading and trailing whitespaces. String str = " Hello "; String trimmedStr = str.trim();
trimmedStr will be "Hello"
startsWith(String prefix) Checks if the string starts with the specified prefix. String str = "Hello"; boolean startsWithHello =
str.startsWith("Hello"); startsWithHello will be true
endsWith(String suffix) Checks if the string ends with the specified suffix. String str = "Hello"; boolean endsWithO =
str.endsWith("o"); endsWithO will be true
contains(CharSequence sequence) Checks if the string contains the specified sequence of String str = "Hello"; boolean containsL =
characters. str.contains("L"); containsL will be false
replace(char oldChar, char Replaces all occurrences of a specified character with String str = "Hello"; String replacedStr = str.replace('l',
newChar) another character. 'L'); replacedStr will be "HeLLo"
replace(CharSequence target, Replaces all occurrences of a specified sequence of String str = "Hello"; String replacedStr = str.replace("l",
CharSequence replacement) characters with another sequence. "L"); replacedStr will be "HeLLo"
replaceFirst(String regex, String Replaces the first substring of the string that matches the String str = "Hello Hello"; String replacedStr =
replacement) given regular expression with the specified replacement. str.replaceFirst("He", "She"); replacedStr will be "Shello
Hello"
replaceAll(String regex, String Replaces each substring of the string that matches the given String str = "Hello 123"; String replacedStr =
replacement) regular expression with the specified replacement. str.replaceAll("\\d", "X"); replacedStr will be "Hello
XXX"
matches(String regex) Checks if the entire string matches the specified regular String str = "Hello"; boolean matches = str.matches("[A-
expression. Za-z]+"); matches will be true
split(String regex) Splits the string into an array of substrings based on the String str = "Hello World"; String[] parts = str.split("\\
specified regular expression. s+"); parts will be ["Hello", "World"]
split(String regex, int limit) Splits the string into an array of substrings based on the String str = "Hello World Java"; String[] parts =
specified regular expression, with a specified limit. str.split("\\s", 2); parts will be ["Hello", "World Java"]
toCharArray() Converts the string to a character array. String str = "Hello"; char[] charArray =
str.toCharArray(); charArray will be ['H', 'e', 'l', 'l', 'o']
getBytes() Encodes the string into a sequence of bytes using the String str = "Hello"; byte[] byteArray = str.getBytes();
platform's default charset. byteArray will contain the encoded bytes
valueOf(primitive data type x) Converts different types of values into a string. int num = 42; String str = String.valueOf(num); str will
be "42"