接下来使用Java Scanner读取文本(模式模式)

时间:2021-01-23 21:51:14

I am trying to use the Scanner class to read a line using the next(Pattern pattern) method to capture the text before the colon and then after the colon so that s1 = textbeforecolon and s2 = textaftercolon.

我试图使用Scanner类使用下一个(模式模式)方法读取一行,以捕获冒号前的文本,然后捕获冒号后的文本,以便s1 = textbeforecolon和s2 = textaftercolon。

The line looks like this:

这条线看起来像这样:

something:somethingelse

3 个解决方案

#1


There are two ways of doing this, depending on specifically what you want.

有两种方法可以做到这一点,具体取决于你想要的。

If you want to split the entire input by colons, then you can use the useDelimiter() method, like others have pointed out:

如果你想用冒号分割整个输入,那么你可以使用useDelimiter()方法,就像其他人指出的那样:

// You could also say "scanner.useDelimiter(Pattern.compile(":"))", but
// that's the exact same thing as saying "scanner.useDelimiter(":")".
scanner.useDelimiter(":");

// Examines each token one at a time
while (scanner.hasNext())
{
    String token = scanner.next();
    // Do something with token here...
}

If you want to split each line by a colon, then it would be much easier to use String's split() method:

如果你想用冒号分割每一行,那么使用String的split()方法会容易得多:

while (scanner.hasNextLine())
{
    String[] parts = scanner.nextLine().split(":");
    // The parts array now contains ["something", "somethingelse"]
}

#2


I've never used Pattern with scanner.

我从来没有使用过模式扫描仪。

I've always just changed the delimeter with a string. http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)

我总是用字符串改变分隔符。 http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)

#3


File file = new File("someFileWithLinesContainingYourExampleText.txt");
Scanner s = new Scanner(file);
s.useDelimiter(":");

while (!s.hasNextLine()) {
    while (s.hasNext()) {
        String text = s.next();
    System.out.println(text);
    }

    s.nextLine();
}

#1


There are two ways of doing this, depending on specifically what you want.

有两种方法可以做到这一点,具体取决于你想要的。

If you want to split the entire input by colons, then you can use the useDelimiter() method, like others have pointed out:

如果你想用冒号分割整个输入,那么你可以使用useDelimiter()方法,就像其他人指出的那样:

// You could also say "scanner.useDelimiter(Pattern.compile(":"))", but
// that's the exact same thing as saying "scanner.useDelimiter(":")".
scanner.useDelimiter(":");

// Examines each token one at a time
while (scanner.hasNext())
{
    String token = scanner.next();
    // Do something with token here...
}

If you want to split each line by a colon, then it would be much easier to use String's split() method:

如果你想用冒号分割每一行,那么使用String的split()方法会容易得多:

while (scanner.hasNextLine())
{
    String[] parts = scanner.nextLine().split(":");
    // The parts array now contains ["something", "somethingelse"]
}

#2


I've never used Pattern with scanner.

我从来没有使用过模式扫描仪。

I've always just changed the delimeter with a string. http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)

我总是用字符串改变分隔符。 http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html#useDelimiter(java.lang.String)

#3


File file = new File("someFileWithLinesContainingYourExampleText.txt");
Scanner s = new Scanner(file);
s.useDelimiter(":");

while (!s.hasNextLine()) {
    while (s.hasNext()) {
        String text = s.next();
    System.out.println(text);
    }

    s.nextLine();
}