第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何將字符串值拆分為多個(gè)值

如何將字符串值拆分為多個(gè)值

函數(shù)式編程 2021-11-11 16:23:11
我有一個(gè)String從 svo 中獲得的值,即String reply=svo.getReplies();我得到的輸出就像--> "1:true,2:false,3:true,4:false,5:false,6:false"現(xiàn)在我想要的是將回復(fù)的存儲(chǔ)分開,并將所有回復(fù)存儲(chǔ)在每個(gè)回復(fù)的新變量中。例如:String firstVal= "true";String secondeVal= "false";// ... and so on. 我該怎么做?
查看完整描述

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));


查看完整回答
反對(duì) 回復(fù) 2021-11-11
?
LEATH

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



查看完整回答
反對(duì) 回復(fù) 2021-11-11
?
MMMHUHU

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

 // ...


查看完整回答
反對(duì) 回復(fù) 2021-11-11
  • 3 回答
  • 0 關(guān)注
  • 168 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)