在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实例。与字符串关联的“包含”功能用于检查原始字符串是否包含特定的子字符串。然后将其打印在屏幕上。
热门推荐
10 送离职同事简短祝福语
11 化学老师元旦祝福语简短
12 新婚祝福语简短十六字
13 表妹结婚祝福语简短藏语
14 上司生日祝福语简短 女性
15 鱼的内涵简短祝福语
16 新年探亲祝福语简短精辟
17 满月红包英文祝福语简短
18 简短佛教新年祝福语大全