如何在 Java 中将分隔符与 Scanner.useDelimiter 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28766377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:46:21  来源:igfitidea点击:

How do I use a delimiter with Scanner.useDelimiter in Java?

javajava.util.scannerdelimiter

提问by NoMoreErrors

sc = new Scanner(new File(dataFile));
sc.useDelimiter(",|\r\n");

I don't understand how delimiter works, can someone explain this in layman terms?

我不明白分隔符是如何工作的,有人可以用外行的术语解释一下吗?

采纳答案by Jordi Castilla

The scanner can also use delimiters other than whitespace.

扫描仪还可以使用空格以外的分隔符。

Easy example from Scanner API:

来自Scanner API 的简单示例:

 String input = "1 fish 2 fish red fish blue fish";

 // \s* means 0 or more repetitions of any whitespace character 
 // fish is the pattern to find
 Scanner s = new Scanner(input).useDelimiter("\s*fish\s*");

 System.out.println(s.nextInt());   // prints: 1
 System.out.println(s.nextInt());   // prints: 2
 System.out.println(s.next());      // prints: red
 System.out.println(s.next());      // prints: blue

 // don't forget to close the scanner!!
 s.close(); 

The point is to understand the regular expressions (regex) inside the Scanner::useDelimiter. Find an useDelimitertutorial here.

问题的关键是要了解正则表达式(regex内)Scanner::useDelimiter在此处查找useDelimiter教程。



To start with regular expressions here you can finda nice tutorial.

要从这里开始使用正则表达式,您可以找到一个不错的教程。

Notes

笔记

abc…    Letters
123…    Digits
\d      Any Digit
\D      Any Non-digit character
.       Any Character
\.      Period
[abc]   Only a, b, or c
[^abc]  Not a, b, nor c
[a-z]   Characters a to z
[0-9]   Numbers 0 to 9
\w      Any Alphanumeric character
\W      Any Non-alphanumeric character
{m}     m Repetitions
{m,n}   m to n Repetitions
*       Zero or more repetitions
+       One or more repetitions
?       Optional character
\s      Any Whitespace
\S      Any Non-whitespace character
^…$     Starts and ends
(…)     Capture Group
(a(bc)) Capture Sub-group
(.*)    Capture all
(ab|cd) Matches ab or cd

回答by cн?dk

With Scanner the default delimiters are the whitespace characters.

对于 Scanner,默认分隔符是空白字符。

But Scanner can define where a token startsand endsbased on a set of delimiter, wich could be specified in two ways:

但是 Scanner 可以根据一组 delimiter定义令牌的开始结束位置,可以通过两种方式指定:

  1. Using the Scanner method: useDelimiter(String pattern)
  2. Using the Scanner method : useDelimiter(Pattern pattern)where Pattern is a regular expression that specifies the delimiter set.
  1. 使用 Scanner 方法:useDelimiter(String pattern)
  2. 使用 Scanner 方法:useDelimiter(Pattern pattern)其中 Pattern 是指定分隔符集的正则表达式。

So useDelimiter()methods are used to tokenize the Scanner input, and behave like StringTokenizer class, take a look at these tutorials for further information:

因此,useDelimiter()方法用于标记 Scanner 输入,其行为类似于StringTokenizer 类,请查看这些教程以获取更多信息:

And here is an Example:

这是一个例子

public static void main(String[] args) {

    // Initialize Scanner object
    Scanner scan = new Scanner("Anna Mills/Female/18");
    // initialize the string delimiter
    scan.useDelimiter("/");
    // Printing the tokenized Strings
    while(scan.hasNext()){
        System.out.println(scan.next());
    }
    // closing the scanner stream
    scan.close();
}

Prints this output:

打印此输出:

Anna Mills
Female
18

回答by Eugenio

For example:

例如:

String myInput = null;
Scanner myscan = new Scanner(System.in).useDelimiter("\n");
System.out.println("Enter your input: ");
myInput = myscan.next();
System.out.println(myInput);

This will let you use Enter as a delimiter.

这将允许您使用 Enter 作为分隔符。

Thus, if you input:

因此,如果您输入:

Hello world (ENTER)

it will print 'Hello World'.

它将打印“Hello World”。