用 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 类中包含 main 函数。在此,定义字符串并创建 CharSequence 实例。使用与字符串关联的 ‘contains’ 函数检查原始字符串中是否包含特定的子字符串。然后将其打印在屏幕上。
Advertisement
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP