gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:52:05 26 4
gpt4 key购买 nike

该行看起来像这样:

某物:某物

最佳答案

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

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

关于java - 接下来用Java Scanner读取文本(Pattern模式),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/842496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com