The String class in Java is used to represent strings of characters. It is one of the most commonly used classes in the Java API. In this post, I will explain the various methods of the String class with examples.
length()
: Returns the length of the string in terms of the number of characters.
Example:
String str = "Hello World"; int length = str.length(); // length is 11
charAt(int index)
: Returns the character at the specified index.
Example:
String str = "Hello World"; char ch = str.charAt(0); // ch is 'H'
substring(int beginIndex, int endIndex)
: Returns a new string that is a substring of the original string. The substring starts at the specified beginIndex and extends to the character at the endIndex – 1 index.
Example:
String str = "Hello World"; String substr = str.substring(0, 5); // substr is "Hello"
concat(String str)
: Concatenates the specified string to the end of the current string.
Example:
String str = "Hello"; String newStr = str.concat(" World"); // newStr is "Hello World"
equals(Object obj):
Compares the current string to the specified object for equality.
Example:
String str1 = "Hello World"; String str2 = "Hello World"; boolean result = str1.equals(str2); // result is true
compareTo(String anotherString)
: Compares the current string to the specified string lexicographically.
Example:
String str1 = "Hello World"; String str2 = "Hello Java"; int result = str1.compareTo(str2); // result is -9
indexOf(int ch)
: Returns the index of the first occurrence of the specified character in the string.
Example:
String str = "Hello World"; int index = str.indexOf('o'); // index is 4
toUpperCase():
Returns a new string in all uppercase.
Example:
String str = "Hello World"; String upper = str.toUpperCase(); // upper is "HELLO WORLD"
toLowerCase()
: Returns a new string in all lowercase.
Example:
String str = "Hello World"; String lower = str.toLowerCase(); // lower is "hello world"
replace(char oldChar, char newChar)
: Returns a new string with all occurrences of the oldChar replaced with the newChar.
Example:
String str = "Hello World"; String newStr = str.replace('o', 'a'); // newStr is "Hella Warld"
trim()
: Returns a new string with all leading and trailing whitespaces removed.
Example:
String str = " Hello World "; String newStr = str.trim(); // newStr is "Hello World"
startsWith(String prefix)
: Returns true if the string starts with the specified prefix.
Example:
String str = "Hello World"; boolean result = str.startsWith("He"); // result is true
endsWith(String suffix)
: Returns true if the string ends with the specified suffix.
Example:
String str = "Hello World"; boolean result = str.endsWith("ld"); // result is true
split(String regex)
: Splits the string into an array of substrings based on the specified regular expression.
Example:
String str = "Hello World"; String[] arr = str.split(" "); // arr is ["Hello", "World"]
valueOf(Object obj)
: Returns the string representation of the specified object.
Example:
int num = 10; String str = String.valueOf(num); // str is "10"
format(String format, Object... args)
: Returns a formatted string using the specified format string and arguments.
Example:
String str = String.format("The value of x is %d and the value of y is %d", 5, 10); // str is "The value of x is 5 and the value of y is 10"
matches(String regex)
: Returns true if the string matches the specified regular expression.
Example:
String str = "Hello World"; boolean result = str.matches(".*World.*"); // result is true
intern()
: Returns a canonical representation for the string object.
Example:
String str1 = "Hello World"; String str2 = new String("Hello World"); boolean result = (str1 == str2.intern()); // result is true
These are some of the most commonly used methods of the String class in Java. There are a few more methods available in the String class, but these should cover most of your needs when working with strings in Java.