日期:2014-05-20 浏览次数:20711 次
String strTemp = "汶川地震,10290, 哄抢救灾物资,9240, 封杀莎朗斯通,3300, 印尼排华是怎么回事,2310, 莎朗斯通,2220, 图片,2130, 朝鲜能不能打败韩国,1800, 图,1590, 杨丞琳辱华惨痛下场,1440, 印尼残害女华人,1440"; String[] strTemps = strTemp.split(","); Map<String,String> words = new HashMap<String,String>(); if(strTemps != null && strTemps.length > 0){ for(int i = 0; i < strTemps.length; i++){ if(i % 2 == 0){ words.put(strTemps[i], strTemps[i+1]); } } }
------解决方案--------------------
for example
import java.util.regex.*; public static void main(String[] args) throws Throwable { class Word { String name; String number; public Word(String name, String number) { this.name = name; this.number = number; } public String toString() { return String.format("name=%s, number=%s", name, number); } }; List<Word> list = new ArrayList<Word>(); String str = "汶川地震,10290, 哄抢救灾物资,9240, 封杀莎朗斯通,3300, 印尼排华是怎么回事,2310, 莎朗斯通,2220, 图片,2130, 朝鲜能不能打败韩国,1800, 图,1590, 杨丞琳辱华惨痛下场,1440, 印尼残害女华人,1440"; Pattern p = Pattern.compile("(\\D+?)[,](\\d+?)([,]\\s+|$)"); Matcher m = p.matcher(str); while (m.find()) { list.add(new Word(m.group(1), m.group(2))); } for (Word w : list) { System.out.println(w); } }