Java String Split splitFields(String fieldsString, int minNum)

Here you can find the source of splitFields(String fieldsString, int minNum)

Description

Utility to split a string on vertical bars into at least minNum pieces.

License

Open Source License

Declaration

public static final String[] splitFields(String fieldsString, int minNum) 

Method Source Code

//package com.java2s;
/*//from  www  .  j av a2 s  . c o  m
  Copyright 2009 Semantic Discovery, Inc. (www.semanticdiscovery.com)
    
  This file is part of the Semantic Discovery Toolkit.
    
  The Semantic Discovery Toolkit is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
    
  The Semantic Discovery Toolkit is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU Lesser General Public License for more details.
    
  You should have received a copy of the GNU Lesser General Public License
  along with The Semantic Discovery Toolkit.  If not, see <http://www.gnu.org/licenses/>.
  */

import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    /**
       * Utility to split a string on vertical bars into at least minNum pieces.
       * <p>
       * Each fields contents will be trimmed and the result will contain at
       * least minNum pieces (with extras if any the empty string, not null).
       */
    public static final String[] splitFields(String fieldsString, int minNum) {
        final String[] pieces = fieldsString.split("\\s*\\|\\s*");
        return ensureNumFields(pieces, minNum);
    }

    /**
       * Utility to split a string on a specified substring into an ArrayList
       * <p>
       * This wraps String.split to return an ArrayList instead of a String[] array.
       * @return an ArrayList containing the string parts
       */
    public static final ArrayList<String> split(String string, String seperator) {
        return new ArrayList<String>(Arrays.asList(string.split(seperator)));
    }

    public static final String[] ensureNumFields(String[] pieces, int minNum) {
        if (pieces != null && pieces.length >= minNum)
            return pieces;

        final int len = (pieces == null) ? 0 : pieces.length;
        final String[] result = new String[minNum];

        for (int i = 0; i < len; ++i)
            result[i] = pieces[i];
        for (int i = len; i < result.length; ++i)
            result[i] = "";

        return result;
    }
}

Related

  1. splitDateText(String text)
  2. splitDigits(long input)
  3. splitDomains(String domains)
  4. splitElements(String values)
  5. splitEx(String str, String spilter)
  6. splitForIndexMatching(String string)
  7. splitGenericIfNeeded(String name)
  8. splitHelperName(String name)
  9. splitHistory(String history)