3

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. 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:

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.

When I test these methods in my regular Java it outputs the expected answer: 4.

I am almost tempted to think this is an actual bug in Android's implementation of the method.

  • 3
    Is there some reason you realllllly need an array of single-character `String`s and not the array of `char`s that you would get by using `word.toCharArray()`? – childofsoong Jul 21 '16 at 20:22
  • 1
    No, and I could try that to see if it works. But it doesnt address the issue that a common method seems to be working differently. I already found a different work-around but I want to know why it doesnt work. – Aleksandr Strizhevskiy Jul 21 '16 at 20:26
  • 4
    This is the expected behavior for `String#split` for Java version up to 7 and has been changed in Java 8: [Why in Java 8 split sometimes removes empty strings at start of result array?](http://stackoverflow.com/q/22718744) – Tom Jul 21 '16 at 20:44
  • Thank you so much Tom, this is exactly what I was looking for! – Aleksandr Strizhevskiy Jul 21 '16 at 20:50

4 Answers4

1

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!

Eenvincible
  • 5,641
  • 2
  • 27
  • 46
1

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.

Anyway, there is many ways to achieve what you want in a normal way, e.g. just iterating over word by each char in a cycle or just use toCharArray String's method.

Divers
  • 9,531
  • 7
  • 45
  • 88
  • There's nothing invalid about the expression. An empty string a valid thing to use in a regular expression. The pipe is actually redundant, however. since it is effectively saying "match on either an empty string or an empty string". – Iain Jul 21 '16 at 21:06
  • @lain I didn't say that it invalid, I said that it doesn't make sense to do that way, it's slow and result unpredictable for 99% of programmers. – Divers Jul 22 '16 at 06:53
1

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().

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(). However, these letters then get put into TextViews and TextView doesnt seem to accept 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?

Apparently the rules for String.split() changed with Java 8.

  • Do you suggest I use toCharArray() and then cast each individual char to String so that I can put it into a textView? That seems even less inefficient. Or is there a better way to get a String broken into individual elements to be placed into textViews? – Aleksandr Strizhevskiy Jul 25 '16 at 02:01
  • Actually it's much much more efficient then usage of Regexp, which always slow. `toCharArray` is very quick native method. If you don't believe me - just make performance tests. And to be honest the biggest issue here, is that your code is very unclear for most of programmers, so your colleagues will not understand why are you doing that. – Divers Jul 25 '16 at 06:16
-1

Try passing a 0 as the limit per the documentation below so that the trailing spaces are discarded.

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.

Joseph Rosson
  • 334
  • 1
  • 8