java 利用Scanner解析逗号字符串

对形如"a,b,c,d,e"类字符串的解析,所用的正则表达式:[\\s*,*\\s]

Scanner sc = new Scanner(System.in);
Pattern p = Pattern.compile("[\\s*,*\\s]"); 
sc.useDelimiter(p);

或者

Scanner sc = new Scanner(System.in);
sc.useDelimiter("[\\s*,*\\s]"); 

A Scanner breaks its input into tokens using a  delimiter pattern, which by default matches whitespace. The resulting  tokens may then be converted into values of different types using the  various next methods。

posted on 2014-03-20 18:08  muyable  阅读(1040)  评论(0编辑  收藏  举报

导航