Pattern类的asPredicate()方法用于创建可用于匹配字符串的谓词对象。谓词是函数接口,因此可用作lambda表达式或方法引用的分配目标。
用法:
public Predicate asPredicate()
参数:此方法不接受任何参数。
返回值:此方法返回可用于匹配字符串的谓词。
以下示例程序旨在说明toString()方法:
示例1:
// Java program to demonstrate
// Pattern.splitAsStream(CharSequence input) method
import java.util.regex.*;
import java.util.function.*;
public class GFG {
public static void main(String[] args)
{
// create a REGEX String
String REGEX = "ee";
// create the string
// in which you want to search
String actualString
= "aaeebbeecceeddee";
// create a Pattern using REGEX
Pattern pattern
= Pattern.compile(REGEX);
// get Predicate Object
Predicate<String> predicate
= pattern.asPredicate();
// check whether predicate match
// with actualString
boolean value = predicate
.test(actualString);
// print result
System.out.println("value matched: "
+ value);
}
}
输出:
value matched: true
示例2:
// Java program to demonstrate
// Pattern.splitAsStream(CharSequence input) method
import java.util.regex.*;
import java.util.function.*;
public class GFG {
public static void main(String[] args)
{
// create a REGEX String
String REGEX = "org";
// create the string
// in which you want to search
String actualString
= "welcome geeks";
// create a Pattern using REGEX
Pattern pattern
= Pattern.compile(REGEX);
// get Predicate Object
Predicate<String> predicate
= pattern.asPredicate();
// check whether predicate match
// with actualString
boolean value
= predicate.test(actualString);
// print result
System.out.println("value matched: "
+ value);
}
}
输出:
value matched: false
参考: https://docs.oracle.com/javase/10/docs/api/java/util/regex/Pattern.html#asPredicate()
相关用法
- Java Pattern pattern()用法及代码示例
- Java Matcher pattern()用法及代码示例
- Java Pattern toString()用法及代码示例
- Java Pattern splitAsStream()用法及代码示例
- Java Pattern flags()用法及代码示例
- Java Pattern compile(String,int)用法及代码示例
- Java Pattern matcher(CharSequence)用法及代码示例
- Java Pattern compile(String)用法及代码示例
- Java Pattern quote(String)用法及代码示例
- Java Pattern split(CharSequence)用法及代码示例
- Java Matcher usePattern(Pattern)用法及代码示例
- Java Pattern split(CharSequence,int)用法及代码示例
- Java Pattern matches(String ,CharSequence)用法及代码示例
- Java Java lang.Long.highestOneBit()用法及代码示例
- Java Java lang.Long.lowestOneBit()用法及代码示例
注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 Pattern asPredicate() Method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。