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.

String s = "Code Factory";
System.out.println(s.charAt(6)); // a
System.out.println(s.charAt(12)); // RE : java.lang.StringIndexOutOfBoundsException

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.

String s = "Code";
s = s.concat(" Factory");
System.out.println(s); // Code Factory

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.

String s1 = "Code";
String s2 = new String("Code");
System.out.println(s1.equals(s2)); // true

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.

String s1 = "Code";
String s2 = new String("CODE");
System.out.println(s1.equalsIgnoreCase(s2)); // true

5. public boolean isEmpty()

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

String s1 = "";
String s2 = new String("CODE");
System.out.println(s1.isEmpty()); // true
System.out.println(s2.isEmpty()); // false

6. public int length()

- Returns the length of this string.

String s1 = "Code";
System.out.println(s1.length()); // 4

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

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

String s1 = "Code";
System.out.println(s1.replace('e', 'o')); // Codo

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.

String s1 = "Code Factory";
System.out.println(s1.substring(5)); // Factory

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.

String s1 = "Code Factory";
System.out.println(s1.substring(5, 9)); // Fact

10. public int indexOf(int ch)

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

String s1 = "Code Factory";
System.out.println(s1.indexOf(111)); // 1
System.out.println(s1.indexOf('o')); // 1
// You can pass ASCII value of Character or Character

11. public int lastIndexOf(int ch)

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

String s1 = "Code Factory";
System.out.println(s1.lastIndexOf(111)); // 9
System.out.println(s1.lastIndexOf('o')); // 9
// You can pass ASCII value of Character or 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()).

String s1 = "Code Factory";
System.out.println(s1.toLowerCase()); // code factory

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()).

String s1 = "Code Factory";
System.out.println(s1.toUpperCase()); // CODE FACTORY

14. public String trim()

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

String s1 = "   code factory  ";
System.out.println(s1.trim()); // code factory