3 回答

TA貢獻(xiàn)1780條經(jīng)驗(yàn) 獲得超5個(gè)贊
你可以用Map這個(gè)字符串制作。然后根據(jù)需要使用該地圖。
例如:String firstVal = map.get(1);
String s1 = "1:true,2:false,3:true,4:false,5:false,6:false";
Map<Integer, String> map = new HashMap<>();
for (String s : s1.split(",")){
map.put(Integer.parseInt(s.substring(0, s.indexOf(":"))), s.substring(s.indexOf(":")+1));
}
for (Integer key : map.keySet()) System.out.println(key + " " + map.get(key));

TA貢獻(xiàn)1936條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以使用正則表達(dá)式來實(shí)現(xiàn):
//Compile the regular expression patern
Pattern p = Pattern.compile("([0-9]+):(true|false)+?") ;
//match the patern over your input
Matcher m = p.matcher("1:true,2:false,3:true,4:false,5:false,6:false") ;
// iterate over results (for exemple add them to a map)
Map<Integer, Boolean> map = new HashMap<>();
while (m.find()) {
// here m.group(1) contains the digit, and m.group(2) contains the value ("true" or "false")
map.put(Integer.parseInt(m.group(1)), Boolean.parseBoolean(m.group(2)));
System.out.println(m.group(2)) ;
}
更多關(guān)于正則表達(dá)式語法的信息可以在這里找到:https : //docs.oracle.com/javase/tutorial/essential/regex/index.html

TA貢獻(xiàn)1834條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以使用Pattern,并Stream在匹配結(jié)果應(yīng)用到Stringreturrned通過svo.getReplies():
String input = "1:true,2:false,3:true,4:false,5:false,6:false";
String[] result = Pattern.compile("(true|false)")
.matcher(input)
.results()
.map(MatchResult::group)
.toArray(String[]::new);
System.out.println(Arrays.toString(result)); // [true, false, true, false, false, false]
String firstVal = result[0]; // true
String secondVal = result[1]; // false
// ...
添加回答
舉報(bào)