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

发布于 2024-07-18 11:27:26 字数 178 浏览 5 评论 0原文

我正在尝试使用 Scanner 类使用 next(Pattern pattern) 方法读取一行,以捕获冒号之前的文本,然后捕获冒号之后的文本,以便 s1 = textbeforecolon 和 s2 = textaftercolon。

该行看起来像这样:

something:somethingelse

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.

The line looks like this:

something:somethingelse

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半衾梦 2024-07-25 11:27:26

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

如果你想用冒号分割整个输入,那么你可以使用 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...
}

如果你想用冒号分割每一行,那么它会很多更容易使用 Stringsplit() 方法:

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

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:

// 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:

while (scanner.hasNextLine())
{
    String[] parts = scanner.nextLine().split(":");
    // The parts array now contains ["something", "somethingelse"]
}
迷你仙 2024-07-25 11:27:26

我从未将 Pattern 与扫描仪一起使用过。

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

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)

海螺姑娘 2024-07-25 11:27:26
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();
}
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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文