Java Scanner问题 [英] Java Scanner question
本文介绍了Java Scanner问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将扫描仪的分隔符设置为;或新行?
How do you set the delimiter for a scanner to either ; or new line?
我试过:
Scanner.useDelimiter(Pattern.compile((\ n)|;) );
但它不起作用。
I tried:
Scanner.useDelimiter(Pattern.compile("(\n)|;"));
But it doesn't work.
推荐答案
作为一般规则,在模式中,你需要加倍 \
。
As a general rule, in patterns, you need to double the \
.
所以,试试
Scanner.useDelimiter(Pattern.compile("(\\n)|;"));`
或
Scanner.useDelimiter(Pattern.compile("[\\n;]"));`
编辑 :如果 \\\\ n
出现问题,您可能需要尝试以下操作:
Edit: If \r\n
is the problem, you might want to try this:
Scanner.useDelimiter(Pattern.compile("[\\r\\n;]+"));
与 \ r
, \ n
,;
。
注意:我没试过这些。
这篇关于Java Scanner问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文