以下是在字符串中搜索字符和子字符串的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的类包含主要功能。在这里,定义了一个字符串,并创建了一个CharSequence实例。与字符串关联的“包含”功能用于检查原始字符串是否包含特定的子字符串。然后将其打印在屏幕上。