使用find()在Java Regexp中查找子序列
该find()方法在输入序列中找到与所需模式匹配的子序列。该方法在java.util.regex包中提供的Matcher类中提供
给出了使用该find()方法在Java中查找子序列的程序,如下所示:
示例
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Demo {
public static void main(String args[]) {
Pattern p = Pattern.compile("cool");
Matcher m = p.matcher("Java is cool");
System.out.println("Subsequence: cool");
System.out.println("Sequence: Java is cool");
if (m.find())
System.out.println("\nSubsequence found");
else
System.out.println("\nSubsequence not found");
}
}输出结果
Subsequence: cool Sequence: Java is cool Subsequence found
现在让我们了解上面的程序。
在字符串序列“Javaiscool”中搜索子序列“cool”。然后使用该find()方法查找子序列是否在输入序列中,并打印所需结果。演示此代码段如下:
Pattern p = Pattern.compile("cool");
Matcher m = p.matcher("Java is cool");
System.out.println("Subsequence: cool" );
System.out.println("Sequence: Java is cool" );
if (m.find())
System.out.println("\nSubsequence found");
else
System.out.println("\nSubsequence not found");热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短