如何在 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
How do I use a delimiter with Scanner.useDelimiter in Java?
提问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 useDelimiter
tutorial 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定义令牌的开始和结束位置,可以通过两种方式指定:
- Using the Scanner method: useDelimiter(String pattern)
- Using the Scanner method : useDelimiter(Pattern pattern)where Pattern is a regular expression that specifies the delimiter set.
- 使用 Scanner 方法:useDelimiter(String pattern)
- 使用 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”。