string handling :: futureX |
String comparison:
There are three ways to compare String objects:
- By equals() method
- By = = operator
- By compareTo() method
By equals() method:
equals() method compares the original content of the string.It compares values of string for equality.String class provides two methods:public boolean equals(Object another){}
compares this string to the specified object.
public boolean equalsIgnoreCase(String another){}
compares this String to another String, ignoring case.
class Simple{
public static void main(String args[]){
String s1="salman";
String s2="salman";
String s3=new String("salman");
String s4="shahrukh";
System.out.println(s1.equals(s2));//true
System.out.println(s1.equals(s3));//true
System.out.println(s1.equals(s4));//false
}
}
By == operator:
The = = operator compares references not values.class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3=new String("Sachin");
System.out.println(s1==s2);//true (because both refer to same instance)
System.out.println(s1==s3);//false(because s3 refers to instance created in nonpool)
}
}
By compareTo() method:
compareTo() method compares values and returns an int which tells if the values compare less than, equal, or greater than.Suppose s1 and s2 are two string variables.If:
s1 == s2 :0
s1 > s2 :positive value
s1 < s2 :negative value
class Simple{
public static void main(String args[]){
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
System.out.println(s1.compareTo(s2));//0
System.out.println(s1.compareTo(s3));//1(because s1>s3)
System.out.println(s3.compareTo(s1));//-1(because s3 < s1 )
}
}
String Concatenation:
There are two ways to concat string objects:
- By + (string concatenation) operator
- By concat() method
By + (string concatenation) operator
String concatenation operator is used to add strings.For Example:
class Simple{
public static void main(String args[]){
String s="Sachin"+" Tendulkar";
System.out.println(s);//Sachin Tendulkar
}
}
The compiler transforms this to:
String s=(new StringBuilder()).append("Sachin").append(" Tendulkar).toString();
String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.String concatenation operator produces a new string by appending the second operand onto the end of the first operand.The string concatenation operator can concat not only string but primitive values also.
For Example:
class Simple{
public static void main(String args[]){
String s=50+30+"Sachin"+40+40;
System.out.println(s);//80Sachin4040
}
}
If either operand is a string, the resulting operation will be string concatenation. If both operands are numbers, the operator will perform an addition.
By concat() method
concat() method concatenates the specified string to the end of current string.Syntax : public String concat(String another){}
class Simple{
public static void main(String args[]){
String s1="Sachin ";
String s2="Tendulkar";
String s3=s1.concat(s2);
System.out.println(s3);//Sachin Tendulkar
}
}
Substring:
You can get substring from the given String object by one of the two methods:
public String substring(int startIndex):
This method returns new String object containing the substring of the given string from specified startIndex (inclusive).
public String substring(int startIndex,int endIndex):
This method returns new String object containing the substring of the given string from specified startIndex to endIndex.
In case of string
startIndex :starts from index 0(inclusive).
endIndex :starts from index 1(exclusive).
class Simple{
public static void main(String args[]){
String s="Sachin Tendulkar";
System.out.println(s.substring(6));//Tendulkar
System.out.println(s.substring(0,6));//Sachin
}
}
No comments:
Post a Comment