java.util.Scanner.skip(Pattern pattern) 方法跳过输入相匹配的指定模式,在忽略分隔符。这个方法会跳过输入,如果锚定的指定模式匹配成功。如果没有找到在当前位置匹配到指定的模式,则没有输入被跳过,抛出NoSuchElementException。
声明
以下是java.util.Scanner.skip()方法的声明
public Scanner skip(Pattern pattern)
参数
-
pattern -- 一个字符串,指定跳过的模式
返回值
此方法返回此scanner
异常
-
NoSuchElementException -- 如果没有找到指定的模式
-
IllegalStateException -- 如果此scanner已关闭
例子
下面的示例演示java.util.Scanner.skip()方法的用法。
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.0 true "; // create a new scanner with the specified String Object Scanner scanner = new Scanner(s); // skip the word that matches the pattern ..llo scanner.skip(Pattern.compile("..llo")); // print a line of the scanner System.out.println("" + scanner.nextLine()); // close the scanner scanner.close(); } }
让我们来编译和运行上面的程序,这将产生以下结果:
World! 3 + 3.0 = 6.0 true