Java String Split splitWS(String s, boolean decode)

Here you can find the source of splitWS(String s, boolean decode)

Description

split WS

License

Apache License

Declaration

public static List<String> splitWS(String s, boolean decode) 

Method Source Code

//package com.java2s;
/*//w  w w.  j a v  a2 s. c  om
 * Copyright 2008-2009 the original ???(zyc@hasor.net).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<String> splitWS(String s, boolean decode) {
        ArrayList<String> lst = new ArrayList<String>(2);
        StringBuilder sb = new StringBuilder();
        int pos = 0, end = s.length();
        while (pos < end) {
            char ch = s.charAt(pos++);
            if (Character.isWhitespace(ch)) {
                if (sb.length() > 0) {
                    lst.add(sb.toString());
                    sb = new StringBuilder();
                }
                continue;
            }
            if (ch == '\\') {
                if (!decode)
                    sb.append(ch);
                if (pos >= end)
                    break; // ERROR, or let it go?
                ch = s.charAt(pos++);
                if (decode) {
                    switch (ch) {
                    case 'n':
                        ch = '\n';
                        break;
                    case 't':
                        ch = '\t';
                        break;
                    case 'r':
                        ch = '\r';
                        break;
                    case 'b':
                        ch = '\b';
                        break;
                    case 'f':
                        ch = '\f';
                        break;
                    }
                }
            }
            sb.append(ch);
        }
        if (sb.length() > 0) {
            lst.add(sb.toString());
        }
        return lst;
    }
}

Related

  1. splitTypes(String fullName)
  2. splitValues(String strValues)
  3. splitValues(String value)
  4. splitVersion(String version)
  5. splitWithoutEscaped(String str, String sep)
  6. splitws(String val)
  7. stringSplit(String s)
  8. stringSplit(String string, String tokens, boolean trimStrings)
  9. stringSplitter(String string)