Escolar Documentos
Profissional Documentos
Cultura Documentos
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.
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)
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;
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
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.
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