3 回答

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]};

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

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));
添加回答
舉報