Java String Split split(String value)

Here you can find the source of split(String value)

Description

Splits the given string to substrings separated by following symbols (one or more): ',', ';', ' ', '\t', '\n', '\r'.

License

Apache License

Parameter

Parameter Description
value the value to split

Return

an array of string values

Declaration

public static String[] split(String value) 

Method Source Code


//package com.java2s;
/* ************************************************************************** *
 * See the NOTICE file distributed with this work for additional information
 * regarding copyright ownership.// ww  w.  j a v a  2  s  .  co m
 * 
 * This file is licensed to you 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 {
    /**
     * Splits the given string to substrings separated by following symbols (one
     * or more): ',', ';', ' ', '\t', '\n', '\r'.
     * 
     * @param value the value to split
     * @return an array of string values
     */
    public static String[] split(String value) {
        List<String> list = new ArrayList<String>();
        if (value != null && !"".equals(value)) {
            char[] array = value.toCharArray();
            StringBuffer buf = new StringBuffer();
            for (char ch : array) {
                if (ch == ',' || ch == ';' || ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r') {
                    if (buf.length() > 0) {
                        list.add(buf.toString());
                        buf.delete(0, buf.length());
                    }
                } else {
                    buf.append(ch);
                }
            }
            if (buf.length() > 0)
                list.add(buf.toString());
        }
        return list.toArray(new String[list.size()]);
    }
}

Related

  1. split(String text)
  2. split(String text, String token)
  3. split(String token, String s)
  4. split(String toSplit)
  5. split(String value)
  6. split_fields(String str)
  7. split_str(String input, String split)
  8. splitAndTrim(String str)
  9. splitAndTrim(String str, String splitBy)