1. Overview
1.概述
In this tutorial, we’ll see how to use the useDelimiter method of the Scanner class.
在本教程中,我们将看到如何使用Scanner类中的useDelimiter方法。
2. Introduction to java.util.Scanner
2.介绍java.util.Scanner
The Scanner API provides a simple text scanner.
Scanner API提供了一个简单的文本扫描仪。
By default, a Scanner splits its input into tokens using white spaces as delimiters. Let’s write a function that will:
默认情况下,扫描器将其输入分成使用空白作为分隔符的标记。让我们写一个函数,它将。
- pass input to a Scanner
- iterate through the Scanner to gather the tokens in a list
Let’s take a look at the basic implementation:
让我们来看看基本的实现。
public static List<String> baseScanner(String input) {
try (Scanner scan = new Scanner(input)) {
List<String> result = new ArrayList<String>();
scan.forEachRemaining(result::add);
return result;
}
}
Let’s note that in this piece of code we’ve used a try-with-resources to create our Scanner. This is possible because the Scanner class implements the AutoCloseable interface. This block takes responsibility for closing the Scanner resource automatically. Before Java 7, we couldn’t use try-with-resources and thus would have had to handle it manually.
让我们注意一下,在这段代码中,我们使用了try-with-resources来创建我们的Scanner。这是可能的,因为Scanner类实现了AutoCloseable接口。这个块负责自动关闭Scanner资源。在Java 7之前,我们不能使用try-with-resources,因此不得不手动处理。
We can also notice that in order to iterate on the Scanner elements, we’ve used the forEachRemaining method. This method was introduced in Java 8. Scanner implements Iterator, and we’d have to take advantage of that to iterate through the elements if we’d used an older Java version.
我们还可以注意到,为了对Scanner元素进行迭代,我们使用了forEachRemaining方法。这个方法是在Java 8中引入的。Scanner实现了Iterator,如果我们使用的是旧的Java版本,我们就必须利用这一点来迭代这些元素。
As we said, Scanner will use white spaces by default to parse its input. For instance, calling our baseScanner method with the following input: “Welcome to Baeldung”, should return a list containing the following ordered elements: “Welcome”, “to”, “Baeldung”.
正如我们所说,Scanner将默认使用空格来解析其输入。例如,在调用我们的baseScanner方法时,有以下输入。”欢迎来到Baeldung”,应该返回一个包含以下有序元素的列表。”欢迎”、”到”、”Baeldung”。
Let’s write a test to check that our method behaves as expected:
让我们写一个测试来检查我们的方法是否有预期的行为。
@Test
void whenBaseScanner_ThenWhitespacesAreUsedAsDelimiters() {
assertEquals(List.of("Welcome", "to", "Baeldung"), baseScanner("Welcome to Baeldung"));
}
3. Use Custom Delimiters
3.使用自定义定界符
Let’s now set up our scanner to use a custom delimiter. We’ll pass in a String which will be used by the Scanner to break the input.
现在让我们来设置我们的扫描器以使用一个自定义的分隔符。我们将传入一个字符串,它将被扫描器用来分割输入。
Let’s see how we can do that:
让我们看看我们如何能够做到这一点。
public static List<String> scannerWithDelimiter(String input, String delimiter) {
try (Scanner scan = new Scanner(input)) {
scan.useDelimiter(delimiter);
List<String> result = new ArrayList<String>();
scan.forEachRemaining(result::add);
return result;
}
}
Let’s comment on a couple of examples:
我们来评论一下几个例子。
- we can use a single character as a delimiter: the character must be escaped if needed. For instance, if we want to mimic the base behavior and use white spaces as delimiters, we’ll use “\\s”
- we can use any word/phrase as a delimiter
- we can use multiple possible characters as delimiters: for this, we have to separate them with a |. For example, if we want to split input between every white space and every line break, we’ll use the following delimiter: “\n|\\s”
- in a nutshell, we can use any kind of regular expression as a delimiter: for instance, “a+” is a valid delimiter
Let’s have a look at how we would test the first case:
让我们来看看我们如何测试第一种情况。
@Test
void givenSimpleCharacterDelimiter_whenScannerWithDelimiter_ThenInputIsCorrectlyParsed() {
assertEquals(List.of("Welcome", "to", "Baeldung"), scannerWithDelimiter("Welcome to Baeldung", "\\s"));
}
Actually, under the scene, the useDelimiter method will convert its input to a regular expression encapsulated in a Pattern object. Alternatively, we could also take care of the instantiation of the Pattern ourselves. For this we would need to use the overriding useDelimiter(Pattern pattern), as shown here:
实际上,在场景下,useDelimiter方法将把其输入转换为regular expression,并封装在Pattern对象中。另外,我们也可以自己处理Pattern的实例化问题。为此,我们需要使用覆盖useDelimiter(Pattern pattern),如下所示。
public static List<String> scannerWithDelimiterUsingPattern(String input, Pattern delimiter) {
try (Scanner scan = new Scanner(input)) {
scan.useDelimiter(delimiter);
List<String> result = new ArrayList<String>();
scan.forEachRemaining(result::add);
return result;
}
}
To instantiate a Pattern, we can use the compile method as in the following test:
要实例化一个Pattern,我们可以使用compile方法,如以下测试。
@Test
void givenStringDelimiter_whenScannerWithDelimiterUsingPattern_ThenInputIsCorrectlyParsed() {
assertEquals(List.of("Welcome", "to", "Baeldung"), DelimiterDemo.scannerWithDelimiterUsingPattern("Welcome to Baeldung", Pattern.compile("\\s")));
}
4. Conclusion
4.总结
In this article, we’ve showcased a couple of examples of patterns that can be used to call the useDelimiter function. We noticed that by default, Scanner uses white space delimiters, and we pointed out that we could use any kind of regular expression there.
在这篇文章中,我们展示了几个可用于调用useDelimiter函数的模式的例子。我们注意到,在默认情况下,Scanner使用空白分隔符,我们指出,我们可以在那里使用任何一种正则表达式。
As always, the complete code is available over on GitHub.
一如既往,完整的代码可在GitHub上获得,。