String Methods
String is a fundamental class in Java for working with text. It provides a wide range of methods for manipulating and inspecting strings. This documentation covers some of the most frequently used methods with real-time examples.
1. length()
length()Method Signature
int length()Description
This method returns the length (the number of characters) of the String.
Example
String text = "Hello, World!";
int length = text.length(); // length will be 132. charAt(int index)
charAt(int index)Method Signature
char charAt(int index)Description
This method returns the character at the specified index in the String.
Example
3. substring(int beginIndex)
substring(int beginIndex)Method Signature
Description
This method returns a new String that is a substring of the original string starting from the specified beginIndex.
Example
4. substring(int beginIndex, int endIndex)
substring(int beginIndex, int endIndex)Method Signature
Description
This method returns a new String that is a substring of the original string, starting from beginIndex (inclusive) and ending at endIndex (exclusive).
Example
5. concat(String str)
concat(String str)Method Signature
Description
This method concatenates the specified string (str) to the end of the original string and returns the resulting String.
Example
6. equals(Object obj)
equals(Object obj)Method Signature
Description
This method compares the content of two strings and returns true if they are equal, false otherwise.
Example
7. equalsIgnoreCase(String anotherString)
equalsIgnoreCase(String anotherString)Method Signature
Description
This method compares two strings, ignoring their case (uppercase/lowercase), and returns true if they are equal, false otherwise.
Example
8. startsWith(String prefix)
startsWith(String prefix)Method Signature
Description
This method checks if the String starts with the specified prefix and returns true if it does, false otherwise.
Example
9. endsWith(String suffix)
endsWith(String suffix)Method Signature
Description
This method checks if the String ends with the specified suffix and returns true if it does, false otherwise.
Example
10. contains(CharSequence sequence)
contains(CharSequence sequence)Method Signature
Description
This method checks if the String contains the specified sequence of characters and returns true if it does, false otherwise.
Example
11. replace(CharSequence target, CharSequence replacement)
replace(CharSequence target, CharSequence replacement)Method Signature
Description
This method replaces all occurrences of the target character sequence with the replacement character sequence and returns the resulting String.
Example
12. trim()
trim()Method Signature
Description
This method removes leading and trailing whitespace from the String and returns the resulting String.
Example
13. split(String regex)
split(String regex)Method Signature
Description
This method splits the String into an array of substrings based on the provided regular expression (regex) and returns the array.
Example
14. toUpperCase() and toLowerCase()
toUpperCase() and toLowerCase()Method Signature
Description
These methods convert all characters in the String to uppercase or lowercase, respectively, and return the resulting String.
Example
15. isEmpty()
isEmpty()Method Signature
Description
This method checks if the String is empty (has a length of 0) and returns true if it is, false otherwise.
Example
These are some of the most frequently used String methods in Java, along with real-time examples. Strings are essential in Java for working with text data, and understanding these methods will help you manipulate and analyze text efficiently in your Java applications.
Last updated
Was this helpful?