java.util.Scanner.findWithinHorizon(Pattern pattern,int horizon)%uA0方法试图找到指定图案的下一次出现。该方法通过输入搜索到指定的搜索地平线,在忽略分隔符。如果找到该模式的scanner 执行匹配的输入,并返回与该模式匹配的字符串。如果没有这样的模式被检测到则返回null,且scanner 的位置保持不变。此方法可能阻塞等待输入相匹配的模式。scanner将永远不会搜索超过超出其当前位置的horizon代码点。请注意,匹配可能被horizon剪裁也就是说,任意匹配的结果可能会有所不同,如果horizon一直较大。
声明
以下是java.util.Scanner.findWithinHorizon()方法的声明
public String findWithinHorizon(Pattern pattern,int horizon)
参数
-
pattern%uA0-- 一个字符串,指定要搜索的模式
返回值
此方法返回匹配指定模式的文本。
异常
-
IllegalStateException%uA0-- 如果此scanner已关闭
-
IllegalArgumentException%uA0-- 如果horizon为负
例子
下面的示例演示java.util.Scanner.findWithinHorizon()方法的用法。
package com.yiibai 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 pattern of 2 letters before rld, with horizon of 10 System.out.println("" + scanner.findWithinHorizon(Pattern.compile("..rld"), 10)) // find a pattern of 2 letters before rld, with horizon of 20 System.out.println("" + scanner.findWithinHorizon(Pattern.compile("..rld"), 20)) // print the rest of the string System.out.println("" + scanner.nextLine()) // close the scanner scanner.close() } }
让我们来编译和运行上面的程序,这将产生以下结果:
null World ! 3+3.0=6