Multiple delimiters in Scanner class of Java

假装没事ソ 提交于 2019-11-28 00:46:05
 Scanner s = new Scanner("hello, world \n hello world");
 s.useDelimiter(",|\\n");
 while(s.hasNext()){
          System.out.println(s.next());

 }

Output

hello
 world 
 hello world

How about useDelimiter(",|\\n");

useDelimiter takes a regex pattern, so, it would be something like ",|\n"

Jigar is absolutely correct. But if it doesn't work, try ",|\\r"

since most text files have \r\n instead of just \n

Using Scan Delimiters for Excel files - don't overlook the RegEx thing. In my case the excel file is delimitedby '|'. I have been spending significant time to parse rows using scanner.useDelimiter("|"), and got back character by character. Replacing that with scanner.useDelimiter("\\|") has solved my issue!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!