length()
length() returns the length of a string. The length is the number of characters present in a String.
Output : 8
equalsIgnoreCase()
equalsIgnoreCase() determines the equality of two Strings, ignoring thier case (upper or lower case doesn't matter ). If two string content is equal then case(I mean upper case and lower case) part is ignoring.
indexOf()
indexOf() returns the index of first occurrence of a substring or a character. indexOf() method has four forms:
Example:
replace()
replace() method replaces occurrences of character with a specified new character.
Output : JAVA
substring()
substring() method returns a part of the string. substring() method has two forms,
The first argument represents the starting point of the subtring. If the substring() method is called with only one argument, the subtring returned, will contain characters from specified starting point to the end of original string.
But, if the call to substring() method has two arguments, the second argument specify the end point of substring.
Output : Race
Output : Rac
toLowerCase()
toLowerCase() method returns string with all uppercase characters converted to lowercase.
Output : javarace
toUpperCase()
This method returns string with all lowercase character changed to uppercase.
Output : JAVARACE
valueOf()
Overloaded version of valueOf() method is present in String class for all primitive data types and for type Object.
NOTE : valueOf() is used to convert primitive data types into Strings.
Output: 65 Silan Technology
But for objects, valueOf() method calls toString().
toString()
toString() method returns the string representation of the object used to invoke this method. toString()is used to represent any Java Object into a meaningful string representation. It is declared in the Object class, hence can be override by any java class.
Output : This is Trilochan 4m Silan Technology
toString() can also be used with normal string objects.
>
Output : java8s.com
Note: If we don't override the toString() method and directly print the object, then it will display the object id.
Example:
output: java8s.Car@15db9742 (here java8s is a user-defined package).
toString() with Concatenation
Whenever we concatenate any other primitive data type, or object of other classes with a String object,toString() or valueOf() is called automatically to change the other object or primitive type into string, for successful concatenation.
trim()
This method returns a string from which any leading and trailing whitespaces has been removed.
Output : java8s
Note: If the whitespaces are between the string ,for example: String s1 = "Silan Technology"; then System.out.println(s1.trim()); will output "Silan Technology".
trim() method removes only the leading and trailing whitespaces.