private String [ ] tokenize ( String s , char separator , int maxTokens ) { List < String > tokens = new ArrayList <> ( ) ; StringBuilder token = new StringBuilder ( ) ; boolean prevIsEscapeChar = false ; for ( int i = 0 ; i < s . length ( ) ; i += Character . charCount ( i ) ) { int currentChar = s . codePointAt ( i ) ; if ( prevIsEscapeChar ) { // Case 1: escaped character token . appendCodePoint ( currentChar ) ; prevIsEscapeChar = false ; } else if ( currentChar == separator && tokens . size ( ) < maxTokens - 1 ) { // Case 2: separator tokens . add ( token . toString ( ) ) ; token = new StringBuilder ( ) ; } else if ( currentChar == ' ' ) { // Case 3: escape character prevIsEscapeChar = true ; } else { // Case 4: regular character token . appendCodePoint ( currentChar ) ; } } if ( token . length ( ) > 0 ) { tokens . add ( token . toString ( ) ) ; } return tokens . toArray ( new String [ ] { } ) ; }
Given a string return an array of tokens . The separator can be escaped with the \ character . The \ character may also be escaped by the \ character .