Multiple delimiters in Scanner class of Java

前端 未结 6 1388
深忆病人
深忆病人 2020-12-03 17:04

How do I use the useDelimiter() method of the Scanner class to use both the comma (,) and the new line character (\\n) as delimiters?

I am

6条回答
  •  广开言路
    2020-12-03 17:52

     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
    
    • JavaDoc

提交回复
热议问题