Java:在相同字符上分割字符串

我想使用“”拆分字符串,但是int main() { struct details *student = details_pointer(n); .. do stuff free(student); } 给我数组中的空字符串,这是我不想要的。

我尝试过:

.split()

输出:

  

[“”,“”,“”,“”,“”,“你好”,等等。...

预期输出:

  

[“ hello”,“ world!”,“ b”,“ y”,“ e”]

我该如何实现?

wuyancindy 回答:Java:在相同字符上分割字符串

首先,修剪字符串,然后用一个或多个空格(\s+)分割。

String string = "     hello       world!     b   y    e   ";
String[] arr = string.trim().split("\\s+");
// output: ["hello","world!","b","y","e"]

String.split() reference

Java regular expressions reference

本文链接:https://www.f2er.com/3170095.html

大家都在问