简体   繁体   English

Java String.split(),如何防止新数组中的空元素

[英]Java String.split(), how to prevent empty element in the new array

I have a String like 我有一个类似的字符串

String s="hello.are..you";
String test[]=s.split("\\.");

The test[] includes 4 elements: 测试[]包括4个元素:

hello
are

you

How to just generate three not empty elements using split()? 如何使用split()生成三个非空元素?

You could use a quantifier 你可以使用量词

String[] array = "hello.are..you".split("\\.+");

To handle a leading . 处理领先. character you could do: 你可以做的角色:

String[] array = ".hello.are..you".replaceAll("^\\.",  "").split("\\.+");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM