Java.util.Scanner.findWithinHorizon() 方法
描述
java.util.Scanner.findWithinHorizon(String pattern,int horizon) 方法尝试查找从指定字符串构造的模式的下一个匹配项,忽略分隔符。调用此方法的形式为 findWithinHorizon (pattern) 的行为方式与调用 findWithinHorizon(Pattern.compile(pattern, Horizon)) 完全相同。
声明
以下是 java.util.Scanner.findWithinHorizon() 方法的声明
public String findWithinHorizon(String pattern,int horizon)
参数
pattern − 指定要搜索的模式的字符串
返回值
此方法返回与指定模式匹配的文本。
异常
IllegalStateException − 如果此扫描仪已关闭
IllegalArgumentException − 如果 horizon 是负数
示例
下面的例子展示了 java.util.Scanner.findWithinHorizon() 方法的使用。
package com.tutorialspoint;
import java.util.*;
import java.util.regex.Pattern;
public class ScannerDemo {
public static void main(String[] args) {
String s = "Hello World! 3 + 3.0 = 6";
// create a new scanner with the specified String Object
Scanner scanner = new Scanner(s);
// find a string of world, with horizon of 10
System.out.println("" + scanner.findWithinHorizon("World", 10));
// find a string of world, with horizon of 20
System.out.println("" + scanner.findWithinHorizon("World", 20));
// print the rest of the string
System.out.println("" + scanner.nextLine());
// close the scanner
scanner.close();
}
}
让我们编译并运行上面的程序,这将产生以下结果 −
null World ! 3 + 3.0 = 6

