在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 好听的元旦简短祝福语