用 Java 在字符串中查找字符和子字符串


以下 Java 程序用于在字符串中搜索字符和子字符串 −

示例

 实时演示

import java.io.*;
import java.lang.*;
public class Demo {
   public static void main (String[] args) {
      String test_str = "Hello";
      CharSequence seq = "He";
      boolean bool_1 = test_str.contains(seq);
      System.out.println("Was the substring found? " + bool_1);
      boolean bool_2 = test_str.contains("Lo");
      System.out.println("Was the substring found? " + bool_2);
   }
}

输出

Was the substring found? true
Was the substring found? False

Demo 类中包含 main 函数。在此,定义字符串并创建 CharSequence 实例。使用与字符串关联的 ‘contains’ 函数检查原始字符串中是否包含特定的子字符串。然后将其打印在屏幕上。

更新于: 2020-09-14

588 次浏览

为您的 事业助力

通过完成课程取得认证

开始学习
Advertisement
© . All rights reserved.