sma*_*i02 48 java regex string split
我想分割一个字符串
"first middle last"
Run Code Online (Sandbox Code Playgroud)
与String.split()
.但当我试图分裂它时,我得到了
String[] array = {"first","","","","middle","","last"}
Run Code Online (Sandbox Code Playgroud)
String.isEmpty()
在我拆分它之后,我尝试使用检查空字符串但是它在android中不起作用.这是我的代码:
String s = "First Middle Last";
String[] array = s.split(" ");
for(int i=0; i<array.length; i++) {
//displays segmented strings here
}
Run Code Online (Sandbox Code Playgroud)
我认为有一种方法可以将它分开:{"first","middle","last"}
但无法弄清楚如何.
谢谢您的帮助!
rid*_*rid 130
由于参数split()
是正则表达式,因此您可以查找一个或多个空格(" +"
)而不只是一个空格(" "
).
String[] array = s.split(" +");
Run Code Online (Sandbox Code Playgroud)
如果你有像这样的字符串
String s = "This is a test string This is the next part This is the third part";
Run Code Online (Sandbox Code Playgroud)
并希望得到一个类似的数组
String[] sArray = { "This is a test string", "This is the next part", "This is the third part" }
Run Code Online (Sandbox Code Playgroud)
你应该试试
String[] sArray = s.split("\\s{2,}");
Run Code Online (Sandbox Code Playgroud)
该{2,}
部分定义了分割发生时至少需要2个且几乎无穷大的空白字符.
小智 7
这对我有用。
s.split(/\s+/)
Run Code Online (Sandbox Code Playgroud)
s.split(/\s+/)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
78285 次 |
最近记录: |