舉一個例子:"Today's food will be fish Tomorrow's food will be soup"
java:如何在空格和大寫字母之間分割字符串?
我想包含
{Today's food will be fish,Tomorrow's food will be soup}
我怎樣才能做到這一點的字符串數組?
舉一個例子:"Today's food will be fish Tomorrow's food will be soup"
java:如何在空格和大寫字母之間分割字符串?
我想包含
{Today's food will be fish,Tomorrow's food will be soup}
我怎樣才能做到這一點的字符串數組?
您可以使用拆分與此正則表達式(?=\s[A-Z])
例如:
String str = "Today's food will be fish Tomorrow's food will be soup";
System.out.println(Arrays.toString(str.split("(?=\\s[A-Z])")));
輸出
[Today's food will be fish, Tomorrow's food will be soup]
試試這個
String a="Today's food will be fish Tomorrow's food will be soup";
String[] b = a.split("\\s(?=[A-Z])");
和b是:今天的食品將是魚,明天的食物將是湯]
爲了支持國際字符,我建議使用以下表達式,建立在YCF_L上:答案。
String str = "Today's food will be fish Tomorrow's food will be soup";
System.out.println(Arrays.toString(str.split("(?=\\s\\p{Lu})")));
的可能的複製[Java的:當找到大寫字母分割字符串(https://stackoverflow.com/questions/3752636/java-split-string-when-an-uppercase-letter-is-找到) –
你到目前爲止嘗試過什麼? – SubOptimal
@billynomates no。如果大寫字母之前的索引是「」或「」,我想分割字符串。 – user8383056