String Methods
Commonly Used String Methods in Java
The String class in Java provides a variety of methods for manipulating strings. Here are some commonly used String methods:
- Length of a String:
length()
Returns the length (number of characters) of a string.
String str = "Hello, World!"; int length = str.length(); // length will be 13 - Accessing Characters:
charAt(int index)
Returns the character at the specified index.
char firstChar = str.charAt(0); // firstChar will be 'H' - Substring:
substring(int beginIndex)andsubstring(int beginIndex, int endIndex)
-substring(int beginIndex): Returns a substring starting from the specified index.
-substring(int beginIndex, int endIndex): Returns a substring frombeginIndex(inclusive) toendIndex(exclusive).
String substring1 = str.substring(7); // substring1 will be "World!" String substring2 = str.substring(0, 5); // substring2 will be "Hello" - Concatenation:
concat(String str)or+operator
-concat(String str): Concatenates the specified string to the end of the current string.
-+operator: Concatenates two strings.
String str1 = "Hello"; String str2 = "World"; String result = str1.concat(", " + str2); // result will be "Hello, World" - Comparison:
equals(String anotherString)andequalsIgnoreCase(String anotherString)
-equals(String anotherString): Compares the content of two strings for equality.
-equalsIgnoreCase(String anotherString): Compares two strings, ignoring case.
String s1 = "Hello"; String s2 = new String("Hello"); boolean isEqual = s1.equals(s2); // isEqual will be true - Search:
indexOf(String str)andlastIndexOf(String str)
-indexOf(String str): Returns the index within the string of the first occurrence of the specified substring.
-lastIndexOf(String str): Returns the index within the string of the last occurrence of the specified substring.
int index1 = str.indexOf("World"); // index1 will be 7 int index2 = str.lastIndexOf("l"); // index2 will be 10 - Changing Case:
toUpperCase()andtoLowerCase()
Converts the entire string to uppercase or lowercase.
String upperCaseStr = str.toUpperCase(); // upperCaseStr will be "HELLO, WORLD!" String lowerCaseStr = str.toLowerCase(); // lowerCaseStr will be "hello, world!" - Trimming:
trim()
Removes leading and trailing whitespaces from the string.
String spacedStr = " Hello, World! "; String trimmedStr = spacedStr.trim(); // trimmedStr will be "Hello, World!" - Replacing Characters:
replace(char oldChar, char newChar)
Replaces all occurrences of a specified character with another character.
String originalStr = "apple"; String replacedStr = originalStr.replace('p', 'b'); // replacedStr will be "abble" - Splitting:
split(String regex)
Splits the string into an array of substrings based on a specified regular expression.
String sentence = "The quick brown fox"; String[] words = sentence.split(" "); // words will be {"The", "quick", "brown", "fox"}
These are just a few examples of the many methods available in the String class. Understanding and using these methods effectively can simplify string manipulation tasks in Java.