bur*_*gar 3 java next java.util.scanner
我试图使用Scanner类使用下一个(模式模式)方法读取一行,以捕获冒号前的文本,然后捕获冒号后的文本,以便s1 = textbeforecolon和s2 = textaftercolon.
这条线看起来像这样:
东西:somethingelse
hbw*_*hbw 10
有两种方法可以实现,具体取决于您的需求.
如果你想用冒号分割整个输入,那么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...
}
Run Code Online (Sandbox Code Playgroud)
如果要分割一个冒号每一行,那么这将是更容易使用String的split()方法:
while (scanner.hasNextLine())
{
String[] parts = scanner.nextLine().split(":");
// The parts array now contains ["something", "somethingelse"]
}
Run Code Online (Sandbox Code Playgroud)