Java
 Computer >> コンピューター >  >> プログラミング >> Java

文字列に部分文字列が含まれているかどうかを確認するJavaプログラム


この記事では、文字列に部分文字列が含まれているかどうかを確認する方法を理解します。文字列は、1つ以上の文字を含み、二重引用符(“”)で囲まれたデータ型です。文字列の一部またはサブセットはサブ文字列と呼ばれます。

以下は同じのデモンストレーションです-

入力がであると仮定します −

The first substring is defined as: Java
The second substring is defined as: C++

必要な出力は

The substring: Java is a part of the defined string.
The substring: C++ is not a part of the defined string.

アルゴリズム

Step 1 - START
Step 2 - Declare three string namely input_string, sub_string_1, sub_string_2
Step 3 - Define the values.
Step 4 - Use the function .contains() to check if the string contains the substring.
Step 5 - Display the result
Step 6 - Stop

例1

ここでは、「main」関数の下ですべての操作をバインドします。

public class Demo {
   public static void main(String[] args) {
      String input_string = "Java Programming";
      System.out.println("The input string is defined as: " +input_string);
      String sub_string_1 = "Java";
      System.out.println("The first substring is defined as: " +sub_string_1);
      String sub_string_2 = "C++";
      System.out.println("The second substring is defined as: " +sub_string_2);
      boolean result = input_string.contains(sub_string_1);
      if(result) {
         System.out.println("The substring: " +sub_string_1 + " is a part of the defined string.");
      } else {
         System.out.println("The substring: " +sub_string_1 + " is not a part of the defined string.");
      }
      result = input_string.contains(sub_string_2);
      if(result) {
         System.out.println("The substring: " +sub_string_2 + " is a part of the defined string.");
      } else {
         System.out.println("The substring: " +sub_string_2 + " is not a part of the defined string.");
      }
   }
}

出力

The input string is defined as: Java Programming
The first substring is defined as: Java
The second substring is defined as: C++
The substring: Java is a part of the defined string.
The substring: C++ is not a part of the defined string.

例2

ここでは、操作をオブジェクト指向プログラミングを示す関数にカプセル化します。

public class Demo {
   static void check_substring(String input_string, String sub_string_1, String sub_string_2){
      boolean result = input_string.contains(sub_string_1);
      if(result) {
         System.out.println("The substring: " +sub_string_1 + " is a part of the defined string.");
      } else {
         System.out.println("The substring: " +sub_string_1 + " is not a part of the defined string.");
      }
      result = input_string.contains(sub_string_2);
      if(result) {
         System.out.println("The substring: " +sub_string_2 + " is a part of the defined string.");
      }else {
         System.out.println("The substring: " +sub_string_2 + " is not a part of the defined string.");
      }
   }
   public static void main(String[] args) {
      String input_string = "Java Programming";
      System.out.println("The input string is defined as: " +input_string);
      String sub_string_1 = "Java";
      System.out.println("The first substring is defined as: " +sub_string_1);
      String sub_string_2 = "C++";
      System.out.println("The second substring is defined as: " +sub_string_2);
      check_substring(input_string, sub_string_1, sub_string_2);
   }
}

出力

The input string is defined as: Java Programming
The first substring is defined as: Java
The second substring is defined as: C++
The substring: Java is a part of the defined string.
The substring: C++ is not a part of the defined string.

  1. 回文をチェックするJavaプログラム

    回文数は、逆にしたときに同じままの数です。たとえば、121、313、525などです。 例 回文をチェックする例を見てみましょう- public class Palindrome {    public static void main(String[] args) {       int a = 525, revVal = 0, remainder, val;       val = a;       System.out.println("Number to be che

  2. 指定された文字列に部分文字列が存在するかどうかを確認するPythonプログラム。

    この問題では、文字列が指定されているため、指定された文字列にサブ文字列が存在するかどうかを確認する必要があります。 アルゴリズム Step 1: input a string and a substring from the user and store it in separate variables. Step 2. Check if the substring is present in the string or not. To do this using find() in-built function. Step 3. Print the final result. Step 4