String Class : Strings created using String object are immutable, i.e. cannot be changed. |
Note : public Boolean equalsIgnoreCase(String anotherString) , compares one string to another string , without considering the case of characters ( upper case and lower case). |
Note-1 : “==” work well with null reference , but equals() will throw NullPointerException. Note-2 : Prefer to use equals() than == to compare Strings for equality,since == compares object references not the content of string. |
Method | Description |
---|---|
1) length() | This method return an integer value which is the length of the String. |
2) charAt(int index) | This method return a char value which is the character at specified index. |
3)indexOf(char ch) | This method returns the index of the first occurrence of specified character. |
3a) indexOf(char ch, int startIndex) | Will return the index of first occurrence of character from the specified startIndex. |
3b) indexOf(String str) | Will return the index of first character of the substring specified. |
3c)indexOf(String str, int startIndex) | Will return the index of first character of the substring specified from the specified index. |
4)substring(int startIndex) | Returns a substring from the specified index to the end of the string. |
4a) substring(int startIndex, int endIndex) | Returns a substring from the startIndex to lastindex-1. |
5) replace(char oldChar, char newChar) | Return a string with replaced characters as specified. |
Method | Description |
---|---|
6)toLowerCase() | Converts string into lower case. |
7)toUpperCase() | Converts string into upper case. |
8)toCharArray() | Converts the string into character array. |
9) isEmpty() | Returns true if string length is 0 else returns false. |
10) valueOf() | This method returns string representation of passed argument. In simple terms this method is used to convert primitive types into String. |
11) trim() | Returns a string with leading and trailing white spaces removed. |
Method | Description |
---|---|
12) lastIndexOf() | This method returns the index of the last occurrence of a character or a substring. |
12a) lastIndexOf(char ch) | To search for the last occurrence of a character and get the index. |
12b) lastIndexOf(char ch, int startIndex) | To search for the last occurrence of a character and get the index from the specified startIndex to 0. |
12c) lastIndexOf(String str) | It searches for the last occurrence of the substring. |
12d) lastIndexof(String str, int startIndex) | Searches for the last occurrence of the substring from the specified startIndex to 0. |
13) startsWith(String str) | Returns true if string starts with specified string else return false. |
13a) startsWith(String str, int startIndex) | Returns true if string from the specified index starts with specified string else returns false. |
14) endsWith(String str) | Returns true if string ends with specified string else returns false. |
15) getBytes() | This method converts the characters in the string to bytes and hence it returns a byte array. Byte array is the ASCII equivalent of each character in string returned as a byte array. |