Java String Split by Length splitStringFixedLen(String data, int interval)

Here you can find the source of splitStringFixedLen(String data, int interval)

Description

split String Fixed Len

License

Open Source License

Declaration

public static String[] splitStringFixedLen(String data, int interval) 

Method Source Code

//package com.java2s;
/**//  w w  w  .  ja  va  2s .c  o m
 * This file is part of Aion X Emu <aionxemu.com>
 *
 *  This is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This software 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 Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser Public License
 *  along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    public static String[] splitStringFixedLen(String data, int interval) {
        List<String> dataPiece = new ArrayList<String>();

        int addedOffset;
        for (int offset = 0; offset < data.length(); offset += addedOffset) {
            String subData = data.substring(offset,
                    Math.min(data.length(), (offset + interval)));
            addedOffset = subData.lastIndexOf('\n');
            if (addedOffset >= 0) {
                subData = subData.substring(0, addedOffset);
                ++addedOffset;
            } else {
                addedOffset = interval;
            }
            dataPiece.add(subData);
        }

        String[] result = new String[dataPiece.size()];
        dataPiece.toArray(result);
        return result;
    }
}

Related

  1. splitString(String str, int length)
  2. splitString(String str, int length)
  3. splitString(String string, int maxCharsPerLine)
  4. splitString(String stringToSplit, int maxLength)
  5. splitStringByLength(String input, int length)
  6. splitWorker(final String str, final String separatorChars, final int max, final boolean preserveAllTokens)
  7. strSplit(String src, int firstLineLength, int otherLineLength)