Powered by Blogger.

String Comparison in java

No comments :
The String class includes a number of methods that compare String or substrings within strings. several are examined here.
equals() and equalsIgnoreCase(): To compare two string for equality, use equals(). It has this general form:

Here, str is the String object being compared with the invoking String object. It returns True if the String contain the same characters in the same order, and false otherwise. The comparison is case-sensitive.
To perform a comparison that ignores case differences, call equalsIgnoreCase(). when it compares two String, it considers A-Z to be the same as a-z. it has this general form:

Here, str is the String object being compared with the invoking String object. It, too, returns true if the string contain the same character in same order, and false otherwise.
Here is an Example that demonstrates equals() and equalsIgnoreCase():

The output form the program is shown here:
                      hello equls hello -> true
                      hello equls good-bay -> false
                      hello equls HELLO -> false
                      hello equalsIgnoreCase HELLO -> true
regionMatches() Method:  The regionMatches() method compares a specific region inside a String with another specific region in another string. There is an overloaded form that allows you to ignore case in such comparisons. Here are the general forms for these two methods:

For both versions, startIndex specifies the index at which the region begins within the invoking String object. The String being compared is specified by str2. the index at which the comparison will start within str2 is specified by str2StarIndex. the length of the substring being compared is passed in numChars. In the second version, if ignore-case is true, the case of the characters is ignored. Otherwise, case is significant. for Example:

Output is:
                               Return Value : true
                               Return Value : false
                               Return Value : false
                               Return Value : false
                              Return Value : true
startsWith() and endsWith() Method : String defines two methods that are, more or less, specialized forms of regionMatches(). The startsWith() method determines whether a given String begins with a specified string. conversely, endsWith() determines whether the String in question ends with a specified string in question ends with a specified String. they have the following general forms:

Here, str is the String being tested. If the String matches, true is returned. Otherwise, false is returned. for Example:


are both true. A second form of starsWith(), shown here, lets you specify a starting point:

Here, startIndex specifies the index into the invoking string at which point the search will begin. For example:
return true.
equals() Versus == :  It is important to understand that the equals() method and the == operator perform two different operation. As just explained, the equals() method compares to see whether they refer to the same instance. The following program shows how two different String objects can contain the same characters. but references to these objects will not compare as equal:

The variable s1 refers to the String instance created by "Hello". The object referred to by s2 is created with s1 as an initializer. Thus, the contents of the two String objects are identical, but they are distinct objects. This means that s1 and s2 do not refer to the same objects and are, therefore, not ==, as is shown here by the output of the preceding example:

CompareTo() : Often, it is not enough to simply know whether two strings are identical. For sorting applications, you need to know which is less than, equal to or greater than the next. A String is less than another if it comes before the other in dictionary order. A string is greater than an other if it comes after the other in dictionary order. A String is Greater Than this purpose. it is specified by the Comparable interface, which String implements. It has the general Form:

Here, str is the String being compared with the invoking String. The result of the comparison is returned and is interpreted as show here:
Value Meaning
Less than Zero The invoking String is less than str
Greater than zero The invoking String is greater than str.
zero The two string are equal.

Here, is a sample program that sorts an array of Strings. The program uses compareTo() to determine sort ordering for a bubble sort:
Outup :
                       Now 
                       aid 
                       all 
                      come 
                      country 
                      for 
                      good 
                      is 
                      men 
                     of 
                     the 
                     the 
                     their 
                     time 
                     to 
                     to 
As you can see from the output of this example, compareTo() takes into account uppercase and lowercase letters. The word "Now" come out before all the others because it begins with an uppercase letter, which means it has a lower value in the ASCII character set. If you want to ignore case differences when comparing two strings use
compareToIgnoreCase(), as shown here:

This method returns the same results as compareTo(), except the case differences are ignored. you might want to try substituting it into the previous program. After doing so, "Now" will no longer be first.

No comments :

Post a Comment

Please Write a Message for Programming or something Related issues.