Java Strings Methods

Java Strings Methods


Java Strings Methods Notes

Strings Methods in Java

  1. char charAt(int) – returns the character at the specified index.
  2. String concat(String) – joins the current string with the specified string object.
  3. boolean endsWith(String) – returns true if the current string ends with the specified string else return false.
  4. boolean startsWith(String) – returns true if the current string starts with the specified string else return false.
  5. boolean equals(String str) – returns true if the current string and str are equal else returns false.
  6. boolean equalsIgnoreCase( String str) - returns true if the current string and str are equal else returns false ignoring case consideration.
  7. int compareTo(String) – compares the current string with the specified string object lexicographically and returns the difference of the ASCII codes of the first dissimilar character. If the strings are equal, it returns 0.
  8. int compareToIgnoreCase(String) – compares the current string with the specified string object lexicographically ignoring case consideration and returns the difference of the ASCII codes of the first dissimilar character. If the strings are equal, it returns 0.
  9. int indexOf(char/String) – returns the index of the first occurrence of the specified character /string in the current string object. If the character/String is not present, it returns -1.
  10. int indexOf(char/String, int) – returns the index of the specified character/string in the current string object starting from the specified index. If the character/string is not present, it returns -1.
  11. int lastIndexOf(char/String) – returns the index of the last occurrence of the specified character/string in the current string object. If the character/string is not present, it returns -1.
  12. int lastIndexOf(char/String, int) – returns the index of the last occurrence of the specified character/string in the current string object starting from the specified index. If the character/string is not present, it returns -1.
  13. int length( ) – returns the number of characters in the current string.
  14. void replace(char oldchar, char newchar) – replaces all occurrences of oldchar in the current string with newchar.
  15. String substring(int startindex, int endindex) – extracts and returns characters from startindex to endindex -1 from the current string object.
  16. toLowerCase( ) – converts the current string object to lower case.
  17. toUpperCase( ) – converts the current string object to upper case.
  18. trim( ) – removes the blank spaces from both ends of the current string object.
  19. valueOf( ) – returns an object of the type of representation desired.