Java.util.Scanner.skip() 方法
描述
java.util.Scanner.skip(Pattern pattern) 方法跳过与指定模式匹配的输入,忽略分隔符。 如果指定模式的锚定匹配成功,则此方法将跳过输入。如果在当前位置未找到与指定模式的匹配,则不跳过任何输入并抛出 NoSuchElementException。
声明
以下是 java.util.Scanner.skip() 方法的声明
public Scanner skip(Pattern pattern)
参数
pattern − 指定要跳过的模式的字符串
返回值
此方法返回此扫描仪
异常
NoSuchElementException − 如果没有找到指定的模式
IllegalStateException − 如果此扫描仪已关闭
示例
下面的例子展示了 java.util.Scanner.skip() 方法的使用。
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.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

