Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình so sánh hai chuỗi trong Java

Trong bài này, chúng ta sẽ hiểu cách so sánh hai chuỗi. So sánh giữa hai chuỗi có thể được thực hiện bằng cách sử dụng toán tử số học ‘==’. Chuỗi là một chuỗi các ký tự. Trong ngôn ngữ lập trình Java, các chuỗi được coi như các đối tượng.

Dưới đây là một minh chứng về điều tương tự -

Giả sử đầu vào của chúng tôi là -

Second string : Java Program
First string :Java

Đầu ra mong muốn sẽ là -

The result of string comparison is: 8

Thuật toán

Step 1 - START
Step 2 - Declare namely
Step 3 - Define the values.
Step 4 - Compute the lengths of the strings. Find the minimum of this length. Assign it to a variable.
Step 5 - Iterate though this value and find the character at every index in both the strings. Convert them to integer, and compare these characters.
Step 6 - Find the integer difference between these characters and return it as the output.
Step 7 - Display the result
Step 8 - Stop

Ví dụ 1

Ở đây, chúng tôi liên kết tất cả các hoạt động với nhau trong hàm "main".

public class CompareStrings {
   public static void main(String args[]) {
      String input_string_1 = new String("Java Program");
      System.out.println("The string is defined as: " +input_string_1);
      String input_string_2 = new String("Java");
      System.out.println("The string is defined as: " +input_string_2);
      int length_1 = input_string_1.length();
      int length_2 = input_string_2.length();
      int minimum_length = Math.min(length_1, length_2);
      int result;
      for (int i = 0; i < minimum_length; i++) {
         int character_1 = (int)input_string_1.charAt(i);
         int character_2 = (int)input_string_2.charAt(i);
         if (character_1 != character_2) {
            result = character_1 - character_2;
         }
      }
      if (length_1 != length_2) {
         result = length_1 - length_2;
      } else {
         result = 0;
      }
      System.out.println("\nThe result of string comparison is: " +result);
   }
}

Đầu ra

The string is defined as: Java Program
The string is defined as: Java

The result of string comparison is: 8

Ví dụ 2

Ở đây, chúng tôi đóng gói các hoạt động thành các hàm thể hiện lập trình hướng đối tượng.

public class CompareStrings {
   public static int string_compare(String input_string_1, String input_string_2) {
      int length_1 = input_string_1.length();
      int length_2 = input_string_2.length();
      int minimum_length = Math.min(length_1, length_2);
      for (int i = 0; i < minimum_length; i++) {
         int character_1 = (int)input_string_1.charAt(i);
         int character_2 = (int)input_string_2.charAt(i);
         if (character_1 != character_2) {
            return character_1 - character_2;
         }
      }
      if (length_1 != length_2) {
         return length_1 - length_2;
      } else {
         return 0;
      }
   }
   public static void main(String args[]) {
      String input_string_1 = new String("Java Program");
      System.out.println("The string is defined as: " +input_string_1);
      String input_string_2 = new String("Java");
      System.out.println("The string is defined as: " +input_string_2);
      System.out.println("\nThe result of string comparison is: " +string_compare(input_string_1, input_string_2));
   }
}

Đầu ra

The string is defined as: Java Program
The string is defined as: Java

The result of string comparison is: 8