Java — Important Methods of String Class | Code Factory

Index Page : Link

Donate : Link

1. public char charAt(int index)

- Returns the char value at the specified index. An index ranges from 0 to length() - 1. The first char value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

- IndexOutOfBoundsException - if the index argument is negative or not less than the length of this string.

2. public String concat(String str)

- Concatenates the specified string to the end of this string.

- If the length of the argument string is 0, then this String object is returned. Otherwise, a String object is returned that represents a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.

3. public boolean equals(Object anObject)

- Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

4. public boolean equalsIgnoreCase(String anotherString)

- Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.

5. public boolean isEmpty()

- Returns true if, and only if, length() is 0.

6. public int length()

- Returns the length of this string.

7. public String replace(char oldChar, char newChar)

- Returns a string resulting from replacing all occurrences of oldChar in this string with newChar.

8. public String substring(int beginIndex)

- Returns a string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.

9. public String substring(int beginIndex, int endIndex)

- Returns a string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is endIndex-beginIndex.

10. public int indexOf(int ch)

- Returns the index within this string of the first occurrence of the specified character.

11. public int lastIndexOf(int ch)

- Returns the index within this string of the last occurrence of the specified character.

12. public String toLowerCase()

- Converts all of the characters in this String to lower case using the rules of the default locale. This is equivalent to calling toLowerCase(Locale.getDefault()).

13. public String toUpperCase()

- Converts all of the characters in this String to upper case using the rules of the default locale. This method is equivalent to toUpperCase(Locale.getDefault()).

14. public String trim()

- Returns a string whose value is this string, with any leading and trailing whitespace removed.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store