View RSS Feed

Compare

Compare strings

  1. Java String comparison with the compareTo method

    by , 04-26-2012 at 08:02 PM
    Also a less common 3rd way for comparison of the Java Strings is present. That method is CompareTo method, with string class. In case of two similar Java Strings, this method will be returning a zero value. String Comparison approach shall look like this given example:

    Java Code:
    String string1 = "foo bar";
    String string2 = "foo bar";
    
    // java string compare example
    if (string1.compareTo(string2) == 0)
    {
      // this line WILL
    ...
    Categories
    Compare
  2. equalsIgnoreCase method

    by , 04-26-2012 at 08:01 PM
    It shall be ignore in Java string comparison tests that whether strings are uppercase of lowercase. Use EqualsIgnoreCase mthods for equality so that to test the strings .
    Java Code:
    String string1 = "foo";
    String string2 = "FOO";
    
    // java string compare while ignoring case
    if (string1.equalsIgnoreCase(string2))
    {
      // this line WILL print
      System.out.println("Ignoring case, the two strings are the same.")
    ...
    Categories
    Compare
  3. Java String comparison with the equals method

    by , 04-26-2012 at 08:00 PM
    About 95% of time, I prefer comparing strings by using Java String class’ equal methods. It is shown below:
    Java Code:
    if (string1.equals(string2))
    String equal method observes if exactly similar string of characters are present in Java strings, they would be considered as equal.
    Quick String comparison could be observed, with equal method, 2 strings wouldn’t be equal as same characters are not present.
    Java Code:
    String string1 = "foo";
    String string2
    ...
    Categories
    Compare
  4. Java String compare to determine Equality

    by , 04-26-2012 at 07:59 PM
    Comparison of the java string could be done in various ways, as given below. It is dependent upon javastringcompare type that which of them will be used.
    * == Operator
    * equals method
    * compareTo method
    Comparing, by using the “== Operator”
    When String object references are compared, ==operator is used. If 2 variables of String are pointed towards similar object present in memory, then comparison will be returned true. In other case it will return false. It shall ...
    Categories
    Compare
  5. Compare strings

    by , 04-26-2012 at 07:58 PM
    Two strings comparison will be learnt in this section. Java lang package gives methods for comparison of 2 with either lower or upper case. Facility is being provided by the equals() method to compare 2 strings. Equal() methods are used by the given program to help you for comparing 2 strings. In case when both of the strings present are equal then a message “Given strings equal” would be displayed. Otherwise, “given strings unequal” would be shown.
    Description of code:
    equals(): ...
    Categories
    Compare