java怎么把提取出来的字符串放到数组Map中?

字符串为:String str="Date: Tue, 14 Sep 2010 13:51:13 GMT" +    "Server: Apache" +    "Cache-Control: max-age=86400";我想以冒号“:”为分割,比如:"Date: Tue, 14 Sep 2010 13:51:13 GMT"分割为Date和Tue, 14 Sep 2010 13:51:13 GMT 两部分,依次类推,不知道能不能实现这样的功能啊! 

第1个回答  2011-09-30
我给你推荐string的几个方法
int indexOf(int ch)
返回指定字符在此字符串中第一次出现处的索引
String substring(int beginIndex, int endIndex)
返回一个新字符串,它是此字符串的一个子字符串。
String[] split(String regex)
根据给定正则表达式的匹配拆分此字符串。
下个api 自己看看就会了
第2个回答  2011-09-27
是不是就第一次出现“:”作为分割,后面的不管了?
你的字符串形式都和这个差不多的吧,给个不是太好的方法:
HashMap<String,String> hm = new HashMap<String, String>();
String s = "Date: Tue, 14 Sep 2010 13:51:13 GMT"+"Server: Apache"+"Cache-Control: max-age=86400";
Pattern p = Pattern.compile("(Date.*?)(Server.*?)(Cache-Control.*)");
Matcher m = p.matcher(s);
while(m.find()) {
String[] test = m.group(1).split(":",2);
hm.put(test[0], test[1]);
test = m.group(2).split(":",2);
hm.put(test[0], test[1]);
test = m.group(3).split(":",2);
hm.put(test[0], test[1]);
}本回答被网友采纳
第3个回答  2011-09-27
str.split(":");用这个方法
相似回答