【发布时间】:2020-04-02 21:17:13
【问题描述】:
我已经使用 .split 函数来完成这项任务,但我注意到当我的数组附近有两个特殊字符时,这里填充了一个空格,这是我的代码
String strLine="cat dog man()home(String[welcome)throws , guess {";
String[] words = strLine.split("\\{+|\\(+|\\[+|\\]+|\\)+|\\,|\\s+");
int len=words.length;
for(int x=0;x<len;x++){
System.out.println(words[x]);
}
我想要没有空格的数组 任何建议
【问题讨论】:
-
提示:字符列表和“一个或多个”量词
-
为什么不直接使用 if 语句呢?
if (!words[x].trim().isEmpty()) { System.out.println(words[x]); } -
@Raghav,我想要没有空格的数组 - 不显示没有空格的数组。