Java 检查字符串是否包含子字符串

Java 检查字符串是否包含子字符串

在Java中,字符串是一个类,用双引号括起来的一系列字符以及该字符串中的连续字符序列被称为子字符串。这些字符实际上是String类型的对象。本文旨在编写Java程序来检查字符串是否包含子字符串。为了检查给定的字符串是否包含子字符串,我们可以使用indexOf()、contains()和substring()方法以及条件语句块。

Java程序:检查字符串是否包含子字符串

我们将在我们的Java程序中使用以下内置方法来检查字符串是否包含子字符串:

  • indexOf()

  • contains()

  • substring()

让我们逐个讨论这些方法,但在此之前,有必要通过一个示例来理解问题陈述。

示例

输入

String = "Simply Easy Learning";
Substring = "Easy";

输出

The string contains the given substring

在上面的示例中,子字符串“Easy”包含在给定字符串“Simply Easy Learning”中。因此,我们得到的输出消息是“字符串包含给定的子字符串”。 现在,让我们讨论一下Java程序,以检查字符串是否包含给定的子字符串。

使用indexOf()方法

String类的indexOf()方法用于在给定字符串中查找指定子字符串的位置。它返回该子字符串的第一个出现的索引,如果未找到则返回-1。

语法

String.indexOf(subString);

示例

以下示例演示了如何使用indexOf()方法来检查字符串是否包含给定的子字符串。

public class Example1 {
   public static void main(String []args) {
      String inputStr = "Simply Easy Learning";
      // Substring to be checked
      String subStr = "Easy";
      System.out.println("The given String: " + inputStr);
      System.out.println("The given Substring: " + subStr);
      // checking the index of substring
      int index = inputStr.indexOf(subStr);
      // to check string contains the substring or not
      if (index != -1) {
         System.out.println("The string contains the given substring");
      } else {
         System.out.println("The string does not contain the given substring");
      }
   }
}

输出

The given String: Simply Easy Learning
The given Substring: Easy
The string contains the given substring

使用contains()方法

contains()方法也是String类的内置方法,用于判断一个字符串是否包含给定的子字符串。它的返回类型是布尔值,如果字符串中包含子字符串,则返回true,否则返回false。

示例

在这个示例中,我们将使用内置方法contains()来检查一个字符串是否包含给定的子字符串。

public class Example2 {
   public static void main(String []args) {
      String inputStr = "Simply Easy Learning";
      // Substring to be checked
      String subStr = "Simply";
      System.out.println("The given String: " + inputStr);
      System.out.println("The given Substring: " + subStr);
      // to check string contains the substring or not
      if (inputStr.contains(subStr)) {
         System.out.println("The string contains the given substring");
      } else {
         System.out.println("The string does not contain the given substring");
      }
   }
}

输出

The given String: Simply Easy Learning
The given Substring: Simply
The string contains the given substring

使用substring()方法

这是String类的另一种方法,用于从给定的字符串中打印子字符串。它接受起始索引和结束索引作为参数,并返回介于这些索引之间的字符。

示例

在下面的示例中,我们将使用substring()方法来查找给定字符串从索引0到2的子字符串。

public class Example3 {
   public static void main(String []args) {
      // initializing the string 
      String inputStr = "Simply Easy Learning";
      System.out.println("The given String is: " + inputStr);
      // Creating a Substring
      String subStr = inputStr.substring(0, 2);
      // printing one of the substring of the given string
      System.out.println("One substring of the given String: " + subStr);
   }
}

输出

The given String is: Simply Easy Learning
One substring of the given String: Si

结论

在本文中,我们学习了什么是字符串和子字符串,以及如何检查一个字符串是否包含给定的子字符串。为了检查指定的字符串中是否包含给定的子字符串,我们使用了Java String类的内置方法indexOf()、contains()和substring()。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程