Vous êtes sur la page 1sur 15

String Comparisons

Arun Sojan MCA 208

equals() To compare two strings for equality Two Possible Outcomes TRUE-IF Equals. FALSE-IF not Equals. CAUTION:They are case sensitive General Format boolean equals(Object str)

equalsIgnoreCase() To compare two strings for equality Two Possible Outcomes TRUE-IF Equals. FALSE-IF not Equals.

NOTE:THEY IGNORE CASE


General Format boolean equalsIgnoreCase(Object str)

Example for equals and equalsIgnorecase()


class equalsDemo { public static void main(String args[]) { String s1 = "Hello"; String s2 = "Hello"; String s3 = "Good-bye"; String s4 = "HELLO"; System.out.println(s1 + " equals " + s2 + " -> " + s1.equals(s2)); System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3)); System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4)); System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " + s1.equalsIgnoreCase(s4)); } } OUTPUT The output from the program is shown here: Hello equals Hello -> true Hello equals Good-bye -> false Hello equals HELLO -> false Hello equalsIgnoreCase HELLO -> true

regionMatches()
This method compares a specific region inside a string with another specific region inside another string . To ignore the case there is an overloaded form. However if one of the following is true then only we get false as an output:if toffset is negative. If ooffset is negative. If toffset + len is greater than the length of the String object. If ooffset + len is greater than the length of the other argument. Parameters: toffset - the starting offset of the subregion in this string. ooffset - the starting offset of the subregion in the string argument. len - the number of characters to compare. General Format is boolean regionMatches(int startIndex, String str2, int str2StartIndex, int numChars) boolean regionMatches(boolean ignoreCase, int startIndex, String str2, int str2StartIndex, int numChars)

Example for regionMatch


public class Test{ public static void main(String args[]){ String Str1 = new String("Welcome to Tutorialspoint.com"); String Str2 = new String("Tutorials"); String Str3 = new String("TUTORIALS");

System.out.print("Return Value :" ); System.out.println(Str1.regionMatches(11, Str2, 0, 9));


System.out.print("Return Value :" ); System.out.println(Str1.regionMatches(11, Str3, 0, 9)); System.out.print("Return Value :" ); System.out.println(Str1.regionMatches(true, 11, Str3, 0, 9)); } } OUTPUT Return Value :true Return Value :false Return Value :true

startsWith()
This is a Boolean type method that returns either 'true' or 'false'. It checks the given string that begins with specified string in the beginning public class StrStartWith{ public static void main(String[] args) { System.out.println("String start with example!"); String str = "Welcome to RoseIndia"; String start = "Wel"; System.out.println("Given String : " + str); System.out.println("Start with : " + start); if (str.startsWith(start)){ System.out.println("The given string is start with Wel"); } else{ System.out.println("The given string is not start with Wel"); } } }

endsWith()
This is a Boolean type method that returns either 'true' or 'false'. It checks the given string that ends with specified string in the beginning General Format is public boolean endsWith(String suffix) Example public class Test{ public static void main(String args[]){ String Str = new String("This is really not immutable!!"); boolean retVal;

retVal = Str.endsWith( "immutable!!" ); System.out.println("Returned Value " + retVal );


retVal = Str.endsWith( "immu" ); System.out.println("Returned Value " + retVal ); } } OUTPUT Returned Value true Returned Value false

equals() and ==
This is a Boolean type method that returns either 'true' or 'false'. It checks the given string that ends with specified string in the beginning General Format is public boolean endsWith(String suffix) Example public class Test{ public static void main(String args[]){ String Str = new String("This is really not immutable!!"); boolean retVal; retVal = Str.endsWith( "immutable!!" ); System.out.println("Returned Value " + retVal ); retVal = Str.endsWith( "immu" ); System.out.println("Returned Value " + retVal ); } } OUTPUT Returned Value true Returned Value false

compareTo()
It is not enough to check weather the strings are identical to each other. SORTING ETC we need to know which I sless than ,equal to,greater than in dictionary order. The value 0 if the argument is a string lexicographically equal to this string; a value less than 0 if the argument is a string lexicographically greater than this string; and a value greater than 0 if the argument is a string lexicographically less than this string. Int comapareTo(String str) Example: public class Test{ public static void main(String args[]) { String str1 = "Strings are immutable"; String str2 = "Strings are immutable"; String str3 = "Integers are not immutable"; int result = str1.compareTo( str2 ); System.out.println(result); result = str2.compareTo( str3 ); System.out.println(result); result = str3.compareTo( str1 ); System.out.println(result); }} OUTPUT WILL BE 0,10,-10

String Searching

indexOf( ) & lastIndexOf()


indexOf( ) Searches for the first occurrence of a character or substring. lastIndexOf( ) Searches for the last occurrence of a character or substring. To search for the first occurrence of a character, use int indexOf(int ch) To search for the last occurrence of a character, use int lastIndexOf(int ch) You can specify a starting point for the search using these forms: int indexOf(int ch, int startIndex) int lastIndexOf(int ch, int startIndex) int indexOf(String str, int startIndex) int lastIndexOf(String str, int startIndex)

startIndex specifies the index at which point the search begins. For indexOf( ), the search runs from startIndex to the end of the string. For lastIndexOf( ), the search runs from startIndex to zero.

// Demonstrate indexOf() and lastIndexOf().


class indexOfDemo { public static void main(String args[]) { String s = "Now is the time for all good men " + "to come to the aid of their country."; System.out.println(s); System.out.println("indexOf(t) = " + s.indexOf('t')); System.out.println("lastIndexOf(t) = " + s.lastIndexOf('t')); System.out.println("indexOf(the) = " + s.indexOf("the")); System.out.println("lastIndexOf(the) = " + s.lastIndexOf("the")); System.out.println("indexOf(t, 10) = " + s.indexOf('t', 10)); System.out.println("lastIndexOf(t, 60) = " + s.lastIndexOf('t', 60)); System.out.println("indexOf(the, 10) = " + s.indexOf("the", 10)); System.out.println("lastIndexOf(the, 60) = " + s.lastIndexOf("the", 60)); } }

OUTPUT
indexOf(t) = 7 lastIndexOf(t) = 65 indexOf(the) = 7 lastIndexOf(the) = 55 indexOf(t, 10) = 11 lastIndexOf(t, 60) = 55 indexOf(the, 10) = 44 lastIndexOf(the, 60) = 55

Vous aimerez peut-être aussi