简体   繁体   English

Java中的scanner skip是什么,为什么要用它?

[英]What is scanner skip in Java and why to use it?

scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?"); 

Can someone explain, what does the above code mean.有人可以解释一下,上面的代码是什么意思。

I'm pretty new to Java.我是 Java 的新手。

Any help would be greatly appreciated.任何帮助将不胜感激。

Scanner.skip

public Scanner skip(Pattern pattern)

Skips input that matches the specified pattern, ignoring delimiters. 跳过与指定模式匹配的输入,忽略定界符。 This method will skip input if an anchored match of the specified pattern succeeds. 如果指定模式的锚定匹配成功,则此方法将跳过输入。
If a match to the specified pattern is not found at the current position, then no input is skipped and a NoSuchElementException is thrown. 如果在当前位置找不到与指定模式的匹配项,则不跳过任何输入,并引发NoSuchElementException。

Since this method seeks to match the specified pattern starting at the scanner's current position, patterns that can match a lot of input (".*", for example) may cause the scanner to buffer a large amount of input. 由于此方法试图从扫描仪的当前位置开始匹配指定的图案,因此可以匹配大量输入(例如“。*”)的图案可能会导致扫描仪缓冲大量输入。

So this allows you to "move" the scanner position using a regex. 因此,这使您可以使用正则表达式“移动”扫描仪位置。

Example : 范例:

Skip the start of the line : 跳过行的开头:

Scanner scan = new Scanner("Hello world");
scan.skip("Hello ");

System.out.println(scan.nextLine());
scan.close();

world 世界

Since this is using a regex, you skip until a work in the middle of a line : 由于这使用的是正则表达式,因此您可以跳过直到一行中间的工作:

Scanner scan = new Scanner("Hello world, I am happy to see you");
scan.skip(".*I am ");

System.out.println(scan.nextLine());
scan.close();

happy to see you 高兴见到你

The skip() method of this class does exactly that. 此类的skip()方法正是这样做的。 It will skip input matching the pattern. 它将跳过与模式匹配的输入。 In this case, the pattern is saying to skip over carriage returns (\\r) and new lines (\\n), plus some unicode caracters. 在这种情况下,该模式表示跳过回车符(\\ r)和换行符(\\ n),以及一些Unicode字符。

So, when the line is read, it will ignore that pattern and return just the rest of the string. 因此,当读取该行时,它将忽略该模式并仅返回字符串的其余部分。 A simpler example would be like this. 一个简单的例子就是这样。 Let's say you have a string: 假设您有一个字符串:

String s = "Hello world, this is my scanner!";

Then if you have a scanner such as: 然后,如果您有扫描仪,例如:

Scanner scan = new Scanner(s);
scan.skip(", this is my scanner");

Then when you do: 然后,当您这样做时:

System.out.println("" + scanner.nextLine());

The output to the console will be simply 控制台的输出将很简单

Hello world!

What happened is the pattern ", this is my scanner" was matched, and ignored from the input. 发生的是模式", this is my scanner"被匹配,并从输入中忽略了。 Then all that was left is the "Hello world!" 然后剩下的就是"Hello world!" string. 串。

import java.util.Scanner;
public class Main {
    public static void main(String[] args) {

                                String s = "Hello world, this is my scanner!";

//Then if you have a scanner such as:

Scanner scan = new Scanner(s);
scan.skip("world");
//Then when you do:

System.out.println("" + scan.nextLine());
scan.close();
    }
}

Output: Exception thrown Output:抛出异常

When I passed Hello to scan.skip(), the code worked and printed everything past Hello.当我将 Hello 传递给 scan.skip() 时,代码起作用并打印了 Hello 之后的所有内容。

You can only pass a prefix to.skip().您只能将前缀传递给 .skip()。 Don't pass a substring starting from anywhere in it.不要从其中的任何地方开始传递 substring。 The string passed has to start the input/or the string S in this case.在这种情况下,传递的字符串必须开始输入/或字符串 S。

Scanner scan = new Scanner(System.in);
scan.skip("a");
//Then when you do:
System.out.println("err");
System.out.println("" + scan.nextLine());
scan.close();

If I enter anything starting with a, it skips a and displays rest of the line.如果我输入以 a 开头的任何内容,它会跳过 a 并显示该行的 rest。

However when I enter anything that doesn't start with a, Then following System.out.println() is never executed and I get stuck at the input.但是,当我输入任何不以 a 开头的内容时,随后的 System.out.println() 永远不会执行,我会卡在输入中。 It won't take input.它不会接受输入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM