private static Iterable < String > splitAndOmitEmpty ( final String str , final char splitChar ) { return new Iterable < String > ( ) { @ Override public Iterator < String > iterator ( ) { return new Iterator < String > ( ) { int startIdx = 0 ; String next = null ; @ Override public boolean hasNext ( ) { while ( next == null && startIdx < str . length ( ) ) { int idx = str . indexOf ( splitChar , startIdx ) ; if ( idx == startIdx ) { // Omit empty string startIdx ++ ; continue ; } if ( idx >= 0 ) { // Found the next part next = str . substring ( startIdx , idx ) ; startIdx = idx ; } else { // The last part if ( startIdx < str . length ( ) ) { next = str . substring ( startIdx ) ; startIdx = str . length ( ) ; } break ; } } return next != null ; } @ Override public String next ( ) { if ( hasNext ( ) ) { String next = this . next ; this . next = null ; return next ; } throw new NoSuchElementException ( "No more element" ) ; } @ Override public void remove ( ) { throw new UnsupportedOperationException ( "Remove not supported" ) ; } } ; } } ; }
Helper method to split a string by a given character with empty parts omitted .