String in JAVA 2
String Class function
The following methods are some of the most commonly used methods of String class.
1) charAt( )
charAt() function returns the character located at the specified index.
String str = "studytonight";
System.out.println(str.charAt(2));
Output : u
2) equalsIgnoreCase( )
equalsIgnoreCase() determines the equality of two Strings, ignoring thier case (upper or lower case doesn't matters with this fuction ).
String str = "java";
System.out.println(str.equalsIgnoreCase("JAVA"));
Output : true
3) length( )
length() function returns the number of characters in a String.
String str = "Count me";
System.out.println(str.length());
Output : 8
4) replace( )
replace() method replaces occurances of character with a specified new character.
String str = "Change myself";
System.out.println(str.replace('my','your'));
Output : Change yourself
5) startsWith() and endsWith( )
It returns boolean either true or false.
startsWith() method checks if this string starts with given prefix
String s="Sachin";
System.out.println(s.startsWith("Sa"));//true
Output : true
endsWith() method checks if this string ends with given suffix.
String s="Sachin";
System.out.println(s.endsWith("ar"));//false
Output : false
6) substring( )
substring() method returns a part of the string. substring() method has two forms.
public String substring(int begin);
public String substring(int begin, int end);
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.
String str = "0123456789";
System.out.println(str.substring(4));
Output : 456789
System.out.println(str.substring(4,7));
Output : 456
7) toLowerCase( )
toLowerCase() method returns string with all uppercase characters converted to lowercase.
String str = "ABCDEF";
System.out.println(str.toLowerCase());
Output : abcdef
8) valueOf( )
Overloaded version of valueOf() method is present in String class for all primitive data types and for type Object.
int a=10;
String s=String.valueOf(a);
System.out.println(s+10);
Output : 1010
NOTE : valueOf() function is used to convert primitive data types into Strings.
9) 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 overrided by any java class. (Object class is super class of all java classes.)
public class Car {
public static void main(String args[])
{
Car c=new Car();
System.out.println(c);
}
public String toString()
{
return "This is my car object";
}
}
Output : This is my car object
Whenever we will try to print any object of class Car, its toString() function will be called. toString() can also be used with normal string objects.
String str = "Hello World";
System.out.println(str.toString());
Output : Hello World
10) toString() with Concatenation
Whenever we concatenate any other primitive data type, or object of other classes with a String object, toString() function or valueOf() function is called automatically to change the other object or primitive type into string, for successful concatenation.
int age = 10;
String str = "He is" + age + "years old.";
In above case 10 will be automatically converted into string for concatenation using valueOf() function
11) toUpperCase( )
This method returns string with all lowercase character changed to uppercase.
String str = "abcdef";
System.out.println(str.toUpperCase());
Output : ABCDEF
12) trim( )
This method returns a string from which any leading and trailing whitespaces has been removed.
String str = " hello ";
System.out.println(str.trim());
Output : hello
No comments:
Post a Comment
Hai , Post your comment . (required, Bugs, Errors )