在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的类包含主要功能。在这里,定义了一个字符串,并创建了一个CharSequence实例。与字符串关联的“包含”功能用于检查原始字符串是否包含特定的子字符串。然后将其打印在屏幕上。
热门推荐
5 短祝福语简短暖心
10 结婚祝福语粤语大全简短
11 晚上祝福语女生文案简短
12 法语妈妈生日祝福语简短
13 药厂开工祝福语大全简短
14 蛋糕节日祝福语简短英文
15 跨年的生日祝福语简短
16 文案祝福语英文短句简短
17 在家聚餐婚礼祝福语简短
18 学生节祝福语大全简短