equalsIgnoreCase()
We use this method to perform the case insensitive comparison between 2 strings. Prototype:
public boolean equalsIgnoreCase(String str)
Example:
String str = "STEVE"; String s = "steve"; System.out.println(str.equals(s)); //output will be "true"
subString()
This method returns the sub string beginning at the given index to the end of the string. Prototype:
public String subString(int begin)
Example:
String str = "Apple"; System.out.println(str.subString(2)); //outpur will be "ple"
subString()
It is the overloaded method that returns the sub string starting and ending-1 from the given index. Prototype:
public String subString(int begin, int end)
Example:
String str = "Apple"; System.out.println(str.subString(2,4)); //outpur will be "pl"
length()
This method returns the length of any string. Prototype:
public int length()
Example:
String str = "Stan"; System.out.println(str.length()); //output will be "4"
replace()
This method replaces the characters in a string with some other given characters. Prototype:
public String replace(char old, char new)
Example:
String str = "Hello"; System.out.println(str.replace('l','H')); //output will be "HeHHo"
toLowerCase()
This method convert the complete string characters to lower case. Prototype:
public String toLowerCase()
Example:
String str = "Hello, How Are You?"; System.out.println(str.toLowerCase()); //Output will be "hello, how are you?"
toUperCase()
This method convert the complete string characters to upper case. Prototype:
public String toUpperCase()
Example:
String str = "Hello, How Are You?"; System.out.println(str.toUpperCase()); //Output will be "HELLO, HOW ARE YOU?"
trim()
This method is for removing the blank spaces present at the beginning and the end of the string and not in its middle. Prototype:
public String trim()
Example:
String str = " this "; System.out.println(str.trim()); //output will be "this"
indexOf()
This method returns the first occurrence of any specified character in a string. Prototype:
public int indexOf(char ch)
Example:
String str = "LOL"; System.out.println(str.indexOf("L")); //output will be "0"
lastIndexOf()
This method returns the last occurence of any specified character in a string. Prototype:
public int lastIndexOf(char ch)
Example:
String str = "LOL"; System.out.println(str.lastIndexOf("L")); //output will be "2"
Importance of String Constant Pool or SCP
In our code, if any String class object is required to be used repeatedly then it is not recommended to create a separate object to fulfill every requirement. Such approach tends to reduce performance and memory utilization. So to solve this problem we can create only one object and share the same object with all required references. This approach can improve memory utilization and performance. But we can only achieve this approach by String Constant Pool.
In SCP, a single object can be shared with all the other references, hence we can say that SCP increases the performance as well as memory utilization. But there is a problem associated with this approach i.e. as several references are attached to the object, if we make any change then all the other references will get change. Hence to resolve this problem, developers at SUN Microsystems made the String class objects as immutable.
So now, once we created a String object, we cannot perform any change in the existing object. And if try to perform any change, then a new object will be created in heap memory, and this will prevent remaining references from being changed. So we can say that disadvantage of SCP is that String class object must be immutable.
Special concept of SCP is for String class only rather than StringBuffer because String class is most common one.