在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 妈妈生日祝福语简短励志