String Methods in Java

This is a guide about String Methods in Java, which are useful tools to manipulate text in programming. Here's a detailed explanation of each method:

  1. length() method: This method returns the number of characters in a string. For example, the string "GeeksforGeeks" has a length of 13.

  2. charAt(int i) method: This method returns the character at a specific index of a string. For instance, "GeeksforGeeks".charAt(3) returns the character 'k' because it is located at index 3.

  3. substring(int i) method: This method returns the substring from the ith index of a string to the end. For example, "GeeksforGeeks".substring(3) returns "ksforGeeks".

  4. substring(int i, int j) method: This method returns the substring from index i to index j-1. For example, "GeeksforGeeks".substring(2, 5) returns "eks".

  5. concat(String str) method: This method concatenates a specified string to the end of another string. For instance, if we have two strings "Geeks" and "forGeeks", we can concatenate them using the concat() method like this: "Geeks".concat("forGeeks"), which returns "GeeksforGeeks".

  6. indexOf(String s) method: This method returns the index of the first occurrence of a specified string within another string. For example, "Learn Share Learn".indexOf("Share") returns 6 because "Share" starts at index 6 in the string.

  7. indexOf(String s, int i) method: This method returns the index of the first occurrence of a specified string within another string, starting at a specified index. For instance, "Learn Share Learn".indexOf("ea", 3) returns 13 because "ea" starts at index 13 in the string, starting from index 3.

  8. lastIndexOf(String s) method: This method returns the index of the last occurrence of a specified string within another string. For example, "Learn Share Learn".lastIndexOf("a") returns 14 because the last occurrence of "a" is at index 14.

  9. equals(Object otherObj) method: This method compares a string to a specified object to see if they are equal. For example, "Geeks".equals("Geeks") returns true because they are the same string.

  10. equalsIgnoreCase(String anotherString) method: This method compares two strings to see if they are equal, ignoring case considerations. For example, "Geeks".equalsIgnoreCase("geeks") returns true because they are the same string, regardless of case.

  11. compareTo(String anotherString) method: This method compares two strings lexicographically (in dictionary order) and returns the difference between them. If the result is less than 0, the first string comes before the second one; if it is 0, the strings are equal, and if it is greater than 0, the first string comes after the second one.

  12. compareToIgnoreCase(String anotherString) method: This method compares two strings lexicographically, ignoring case considerations.

  13. toLowerCase() method: This method converts all the characters in a string to lower case. For example, "HeLLo".toLowerCase() returns "hello".

  14. toUpperCase() method: This method converts all the characters in a string to upper case. For example, "HeLLo".toUpperCase() returns "HELLO".

  15. trim() method: This method returns a copy of a string by removing white spaces from both ends. It does not affect white spaces in the middle.

Previous Post Next Post