Java String Split by Space split(String source, String limit, boolean trim, boolean ignoreWhitespace)

Here you can find the source of split(String source, String limit, boolean trim, boolean ignoreWhitespace)

Description

split

License

Open Source License

Parameter

Parameter Description
source a parameter
limit a parameter
trim a parameter
ignoreWhitespace a parameter

Return

String[]

Declaration

public static String[] split(String source, String limit, boolean trim, boolean ignoreWhitespace) 

Method Source Code


//package com.java2s;
/*//from ww w .j a v  a  2 s  . c o  m
 * $RCSfile: StringUtil.java,v $$
 * $Revision: 1.1 $
 * $Date: 2013-03-02 $
 *
 * Copyright (C) 2008 Skin, Inc. All rights reserved.
 *
 * This software is the proprietary information of Skin, Inc.
 * Use is subject to license terms.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * @param source
     * @param limit
     * @param trim
     * @param ignoreWhitespace
     * @return String[]
     */
    public static String[] split(String source, String limit, boolean trim, boolean ignoreWhitespace) {
        int i = 0;
        int j = 0;
        String s = null;
        List<String> list = new ArrayList<String>();

        while ((j = source.indexOf(limit, i)) > -1) {
            if (j > i) {
                s = source.substring(i, j);

                if (trim) {
                    s = s.trim();
                }

                if (!ignoreWhitespace || s.length() > 0) {
                    list.add(s);
                }
            }
            i = j + limit.length();
        }

        if (i < source.length()) {
            s = source.substring(i);

            if (trim) {
                s = s.trim();
            }

            if (!ignoreWhitespace || s.length() > 0) {
                list.add(s);
            }
        }
        String[] result = new String[list.size()];
        return list.toArray(result);
    }

    /**
     * @param source
     * @param length
     * @param padding
     * @return String
     */
    public static String substring(String source, int length, String padding) {
        if (source == null) {
            return "";
        }

        String s = source.trim();

        char c;
        int size = 0;
        int count = s.length();
        StringBuilder buffer = new StringBuilder();

        for (int i = 0; i < s.length(); i++) {
            c = s.charAt(i);

            if (c >= 0x0080) {
                size += 2;
                count++;
            } else {
                size++;
            }

            if (size > length) {
                if (c >= 0x4e00) {
                    size -= 2;
                } else {
                    size--;
                }

                break;
            }

            buffer.append(c);
        }

        if (size < count && padding != null) {
            buffer.append(padding);
        }
        return buffer.toString();
    }
}

Related

  1. splitAndKeepEscapedSpaces(String string, boolean preserveEscapes)
  2. splitAtSpaces(String s)
  3. splitBySpace(String p_str)
  4. splitInWhiteSpaces(String string)