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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java:如何在空格后將字符串拆分為兩個單獨的數(shù)組?

Java:如何在空格后將字符串拆分為兩個單獨的數(shù)組?

藍(lán)山帝景 2021-11-24 16:11:39
如果客戶端想要將消息存儲到 txt 文件中,用戶可以使用關(guān)鍵字 msgstore 后跟引號。示例: msgstore“ABC 就像 123 一樣簡單”我正在嘗試將 msgstore 和引號拆分為數(shù)組中的兩個單獨元素。我目前擁有的是:String [] splitAdd = userInput.split(" ", 7);但我面臨的問題是它在第二個空格后再次分裂,因此它是:splitAdd[0] = msgstoresplitAdd[1] = "ABCsplitAdd[2] = is我的問題是,如何將后半部分組合成一個元素長度未知的單個數(shù)組,以便它是:splitAdd[0] = msgstoresplitAdd[1] = "ABC is as easy as 123"我是 Java 新手,但我知道在 python 中使用 (7::) 之類的東西很容易做到。有什么建議嗎?
查看完整描述

3 回答

?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗 獲得超9個贊

當(dāng)你只需要 2 個元素時,為什么你有limit參數(shù)7?嘗試將其更改為2:-

String splitAdd[] = s.split(" ", 2);

或者

String splitAdd[] = new String[]{"msgstore", s.split("^msgstore ")[1]};


查看完整回答
反對 回復(fù) 2021-11-24
?
幕布斯7119047

TA貢獻(xiàn)1794條經(jīng)驗 獲得超8個贊

第一個 indexOf 上的子字符串 "


String str = "msgstore \"ABC is as easy as 123\"";


int ind = str.indexOf(" \"");


System.out.println(str.substring(0, ind));

System.out.println(str.substring(ind));

編輯


如果這些值需要在一個數(shù)組中,那么


String[] arr = { str.substring(0, ind), str.substring(ind)};


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

TA貢獻(xiàn)1834條經(jīng)驗 獲得超8個贊

您可以使用正則表達(dá)式:演示


Pattern pattern = Pattern.compile("(?<quote>[^\"]*)\"(?<message>[^\"]+)\"");

Matcher matcher = pattern.matcher("msgstore \"ABC is as easy as 123\"");


if(matcher.matches()) {

    String quote = matcher.group("quote").trim();

    String message = matcher.group("message").trim();

    String[] arr = {quote, message};


    System.out.println(Arrays.toString(arr));

}

這比 substring 一個字符串更具可讀性,但它顯然更慢。作為替代方案,您可以使用 substirng 字符串:


String str = "msgstore \"ABC is as easy as 123\"";

int pos = str.indexOf('\"');

String quote = str.substring(0, pos).trim();

String message = str.substring(pos + 1, str.lastIndexOf('\"')).trim();

String[] arr = { quote, message };

System.out.println(Arrays.toString(arr));


查看完整回答
反對 回復(fù) 2021-11-24
  • 3 回答
  • 0 關(guān)注
  • 273 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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