The following methods are most commonly used methods of String class.
charAt() returns the character located at the specified index.
Output : R
Note: Index of a String starts from 0, hence str.charAt(4) means fifth character of the String str.
length() returns the length of a string. The length is the number of characters present in a String.
Output : 8
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.
Output : true
indexOf() returns the index of first occurrence of a substring or a character. indexOf() method has four forms:
- int indexOf(String str) It returns the index within this string of the first occurrence of the specified substring.
- int indexOf(int ch, int fromIndex) It returns the index within this string of the first occurrence of the specified character, starting the search at the specified index.
- int indexOf(int ch) It returns the index within this string of the first occurrence of the specified character.
- int indexOf(String str, int fromIndex) It returns the index within this string of the first occurrence of the specified substring, starting at the specified index.
Note: here -1 represents that the substring/Character is not found in the given String.
replace() method replaces occurrences of character with a specified new character.
Output : JAVA
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() method returns string with all uppercase characters converted to lowercase.
Output : javarace
This method returns string with all lowercase character changed to uppercase.
Output : JAVARACE
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() 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.
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.
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.
Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Data Science with Python Training | Projects
SILAN Technology, BBSR Call to: 0674-2361252