In Java, sc.skip(pattern) skips over text that matches patternstarting at the exact current position, ignoring any delimiter setting, and returns the Scanner itself.
Unlike findInLine(), skip() does not search forward: if the text right at the cursor does not match, it throws NoSuchElementException.
An instance method of java.util.Scanner, available since Java 5 (JDK 1.5). A Scanner reads from any source (System.in for keyboard, a String, or a file) with identical parsing logic; pass the source to new Scanner(...) after import java.util.Scanner; and call sc.skip(...).
Syntax
Pass a regex String or Pattern that matches the prefix sitting directly at the scanner's cursor.
Java
Scanner sc = new Scanner("ID: 42");
int id = sc.skip("ID:\\s*").nextInt(); // skips "ID: ", reads 42
Call
What it does
sc.skip("ID:\\s*")
Matches right at the cursor, advances past it, and returns this.
sc.skip(pattern)
Matches using a compiled Pattern object.
sc.skip("\\R")
Skips an expected line break without allocating a line string.
Because skip() returns the Scanner, you can chain sc.skip("prefix:").nextInt() in one readable expression. It also populates match().
Library declarations
Java
// java.util.Scanner
public Scanner skip(Pattern pattern)
public Scanner skip(String pattern)
Parameters
Parameter
Description
pattern
Regex String or Pattern anchored at the current scanner position.
Return Value
Return Value
Description
Scanner
The same scanner instance, positioned right after the skipped match.
Throws NoSuchElementException
If the input at the current position does not match pattern.