Java String Split by Line SplitLine(String p_line, String p_delimiter)

Here you can find the source of SplitLine(String p_line, String p_delimiter)

Description

Split Line

License

Open Source License

Declaration

static public Vector<String> SplitLine(String p_line, String p_delimiter) 

Method Source Code


//package com.java2s;
/*/*from   w  w w  .ja v  a  2  s  .  c  o m*/
   Copyright 2016 Wes Kaylor
    
   This file is part of CoreUtil.
    
   CoreUtil 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.
    
   CoreUtil 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 CoreUtil.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.*;

public class Main {
    static public Vector<String> SplitLine(String p_line, String p_delimiter) {
        Vector<String> t_values = new Vector<String>();
        String t_newValue;
        int t_valueIndex = 0;
        int t_lastDelimiterIndex = -1;
        int t_nextDelimiterIndex = p_line.indexOf(p_delimiter);

        while (t_nextDelimiterIndex >= 0) {
            t_values.add(t_valueIndex++, p_line.substring(t_lastDelimiterIndex + 1, t_nextDelimiterIndex).trim());
            t_lastDelimiterIndex = t_nextDelimiterIndex;
            t_nextDelimiterIndex = p_line.indexOf(p_delimiter, t_lastDelimiterIndex + 1);
        }

        // Get the last value in the line.
        if (t_lastDelimiterIndex > 0) {
            if (t_lastDelimiterIndex == (p_line.length() - 1)) {
                t_values.add(t_valueIndex, null); // The last field is empty, so we'll just set this value as an empty string so that everything works.
            } else {
                t_newValue = p_line.substring(t_lastDelimiterIndex + 1).trim();

                if (t_newValue.length() > 0)
                    t_values.add(t_valueIndex, t_newValue);
                else
                    t_values.add(t_valueIndex, null);
            }
        }

        return t_values;
    }
}

Related

  1. splitIntoLines(String str)
  2. splitIntoNonEmptyLines(String s)
  3. splitIntoNonEmptyLinesWithoutNetLogoComments( String s)
  4. splitLine(final String line, final char delimiter, final char qualifier, final int initialSize)
  5. splitLine(String input, int splitPosition)
  6. splitLines(final String helpMessage)
  7. splitLines(final String string)
  8. splitLines(String raw, int limit)
  9. splitLines(String S)