Saturday 11 October 2014

Compare two String in Java

Structure of the Problem Requirements

In this Problem we will discuss about String built in function. we discuss about compare function in string which is compare the two strings and tell that which string is greater. If first string is greater then its display that first string greater then second. first enter two string and then we compare it.Here is the source code of this problem which help you in better understanding.


SOURCE CODE



import java.util.Scanner;

public class LinkedList
{
   public static voidmain(String args[])
   {
      Stringf_String;
      String s_String;
      Scanner in = new Scanner(System.in);

      System.out.println("Enter First String :");
      f_String = in.nextLine();

      System.out.println("Enter Second String :");
      s_String = in.nextLine();

      if ( f_String.compareTo(s_String) > 0 )
      {
         System.out.println("First String Less Than Second ! ");
      }
      else if ( f_String.compareTo(s_String) < 0 )
      {
          System.out.println("First String Greater Than Second ! ");
      }
      else
      {
         System.out.println("Strings Equal !");
      }
  
   }

}

OUTPUT OF THE PROGRAM


Java String Functions
Compare Strings

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

1 comments: Post Yours! Read Comment Policy!▼
Important Note:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.

  1. Use the compareTo technique, if you're fascinated by the authorship order of the strings - that is "less", that is "greater". If you would like to kind the strings, or quickly wanting during a sorted array of strings, you would like all three doable come values - less, equal or larger -, therefore store the results of the compareTo() in associate number variable:
    int res = str1.compareTo(str2);
    if (res == 0) {
    // strings are equal
    } else if (res < 0) {
    // str1 is before str2
    }

    ReplyDelete