简体   繁体   English

string.split()并处理重复项

[英]string.split() and dealing with duplicates

I have a string String test = "-b--b--"; 我有一个字符串String test = "-b--b--";

I want an array that has {"", "b", "", "b", ""} 我想要一个具有{"", "b", "", "b", ""}的数组

How can I call the string.split() function to obtain this? 如何调用string.split()函数来获取此信息?

EDIT: 编辑:

I've tried test.split("-") . 我已经尝试过test.split("-") My tests gave me {"", "b", "", "b"} . 我的测试给了我{"", "b", "", "b"} Why does the method not include the last emptyspace on my array if it did include the first one between the first pair of hyphens? 如果该方法在第一对连字符之间包含第一个空格,为什么该方法不包括数组中的最后一个空格?

Basically I want to get all the characters between the hypens, where String test = --- should give me {"", "", ""} 基本上我想得到连字符之间的所有字符,其中String test = ---应该给我{"", "", ""}

Its mentioned in the docs 它在文档中提到

Trailing empty strings not included in the resulting array. 结果数组中不包含结尾的空字符串。


For the desired result change it to 为了获得所需的结果,将其更改为

test.split("-",Integer.MAX_VALUE);

This would consider empty strings and so the output would be 这将考虑空字符串,因此输出将是

"", "b", "", "b", "" , ""

you can try .getCharAt() method of string to your problem. 您可以尝试字符串的.getCharAt()方法解决您的问题。
The following code can help: 以下代码可以帮助您:

char test1[]=new char[test.length()];
for(int i=0;i<test.length();i++)
if(test.getCharAt(i)=='-')
test1[i]=' ';
else
test[i]=test.getCharAt(i);  

hope this will help you.... 希望这个能对您有所帮助....

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

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