这是一个示例字符串,我打算将其拆分为一个数组:
Hello My Name Is The Mighty Llama
Run Code Online (Sandbox Code Playgroud)
输出应该是:
Hello My
Name Is
The Mighty
Llama
Run Code Online (Sandbox Code Playgroud)
以下分裂在每个空间,我如何分裂每个其他空间?
String[] stringArray = string.split("\\s");
Run Code Online (Sandbox Code Playgroud)
你可以这样做:
String[] stringArray = string.split("(?<!\\G\\S+)\\s");
Run Code Online (Sandbox Code Playgroud)