简体   繁体   English

Android String.split(“”)返回额外的元素

[英]Android String.split(“”) returning extra element

I am trying to split a word into its individual letters. 我试图将一个单词分成单独的字母。

I tried both String.split("") and String.split("|") however when I split a word it is creating a extra empty element. 我尝试了String.split(“”)和String.split(“|”)但是当我分割一个单词时,它正在创建一个额外的空元素。 Example: 例:

    word = "word";
    int n = word.length();
    Log.i("20",Integer.toString(n));
    String[] letters = word.split("|");
    Log.i("25",Integer.toString(letters.length));

The output in the Android Monitor is: Android Monitor中的输出是:

07-21 15:50:23.084 5711-5711/com.strizhevskiy.movetester I/20: 4
07-21 15:50:23.085 5711-5711/com.strizhevskiy.movetester I/25: 5

I put the individual letters into TextView blocks and I can actually see an extra empty TextView. 我将单个字母放入TextView块中,我实际上可以看到一个额外的空TextView。

When I test these methods in my regular Java it outputs the expected answer: 4. 当我在常规Java中测试这些方法时,它会输出预期的答案:4。

I am almost tempted to think this is an actual bug in Android's implementation of the method. 我几乎想到这是Android实现该方法的一个实际错误。

I am thinking you want to do this: 我在想你想这样做:

public Character[] toCharacterArray( String s ) {

   if ( s == null ) {
       return null;
   }

   int len = s.length();
   Character[] array = new Character[len];
   for (int i = 0; i < len ; i++) {
      array[i] = new Character(s.charAt(i));
   }

   return array;
}

Instead of splitting a word without delimiters? 没有分隔符而不是拆分单词?

I hope this helps! 我希望这有帮助!

It's hard to say if it's bug or expected behavior, because what are you doing doesn't make sense. 很难说它是错误还是预期的行为,因为你做的事情没有意义。 You are trying to split string with logical OR ( split is waiting for Regular expression , not just a string), so as result it could be different result in Android comparing with normal java, and I don't see there any issue. 您正在尝试使用逻辑OR拆分字符串( split正在等待Regular expression ,而不仅仅是字符串),因此在Android中与普通java相比可能会有不同的结果,我没有看到任何问题。

Anyway, there is many ways to achieve what you want in a normal way, eg just iterating over word by each char in a cycle or just use toCharArray String's method. 无论如何,有很多方法可以以正常的方式实现你想要的东西,比如只是在一个循环中迭代每个字符或者只是使用toCharArray String的方法。

Thank you for the suggestions. 感谢您的建议。 My current work-around is to use a mock array and copying over into a fresh array using System.arraycopy(). 我目前的解决方法是使用模拟数组并使用System.arraycopy()复制到新数组中。

String[] mockLetters = word.split("");
int n = word.length();
String[] letters = new String[n];

System.arraycopy(mockLetters,1,letters,0,n);

I appreciate the suggestions to use toCharArray(). 我很欣赏使用toCharArray()的建议。 However, these letters then get put into TextViews and TextView doesnt seem to accept char. 然而,这些字母然后被放入TextViews并且TextView似乎不接受char。 I could, of coarse, make it work but I've decided to stick with what I currently have. 我可以,粗糙,使它工作,但我决定坚持我现在拥有的。

Tom, in a comment to my question, answered my underlying issue: Why String.split() worked differently in Android than it does in Java? Tom在评论我的问题时回答了我的根本问题:为什么String.split()在Android中的工作方式与在Java中的工作方式不同?

Apparently the rules for String.split() changed with Java 8. 显然,String.split()的规则随Java 8而改变。

Try passing a 0 as the limit per the documentation below so that the trailing spaces are discarded. 尝试传递0作为下面文档的限制,以便废弃尾随空格。

String[] split (String regex, int limit) String [] split(String regex,int limit)

If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded. 如果n为零,那么模式将被应用尽可能多的次数,该数组可以具有任何长度,并且将丢弃尾随的空字符串。

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

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