英文:
split method in java
问题
这是我的代码,用于将给定字符串拆分并放入数组中。
问题是,我希望在传递空字符串时也能正常工作。有什么办法实现这个?
String[] buArray = buList.split(";",-1);
所以我希望在传递 "" 时 `buArray.length` 等于零。
英文:
Here is my code to split given string and put them into an array.
Question, I want it to work while I am passing an empty String. How is that possible?
String[] buArray = buList.split(";", -1);
So I want buArray.length equals zero while passing "".
答案1
得分: 1
可以使用类似以下方式:
String[] buArray =
  (buList == null || buList.isEmpty())
  ? new String[0]: buList.split(";");
如果不需要的话,可以去掉空值检查。
英文:
You can use something like below:
String[] buArray = 
  (buList == null || buList.isEmpty()) 
  ? new String[0]: buList.split(";");
You can remove null check if it is not needed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论