Java String Split splitAtLastBlank(String s, int width)

Here you can find the source of splitAtLastBlank(String s, int width)

Description

Splits the specified string at the last blank before width.

License

Open Source License

Parameter

Parameter Description
s the string to be split
width int

Return

string fragments

Declaration

public static List<String> splitAtLastBlank(String s, int width) 

Method Source Code

//package com.java2s;
/*//from  w  w w.  ja v  a  2  s  .c  om
 This file is part of ELKI:
 Environment for Developing KDD-Applications Supported by Index-Structures
    
 Copyright (C) 2015
 Ludwig-Maximilians-Universit?t M?nchen
 Lehr- und Forschungseinheit f?r Datenbanksysteme
 ELKI Development Team
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 This program 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 Affero General Public License for more details.
    
 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * The system newline setting.
     */
    public static final String NEWLINE = System.getProperty("line.separator");

    /**
     * Splits the specified string at the last blank before width. If there is no
     * blank before the given width, it is split at the next.
     *
     * @param s the string to be split
     * @param width int
     * @return string fragments
     */
    public static List<String> splitAtLastBlank(String s, int width) {
        List<String> chunks = new ArrayList<>();

        String tmp = s;
        while (tmp.length() > 0) {
            int index = findSplitpoint(tmp, width);
            // store first part
            chunks.add(tmp.substring(0, index));
            // skip whitespace at beginning of line
            while (index < tmp.length() && tmp.charAt(index) == ' ') {
                index += 1;
            }
            // remove a newline
            if (index < tmp.length() && tmp.regionMatches(index, NEWLINE, 0, NEWLINE.length())) {
                index += NEWLINE.length();
            }
            if (index >= tmp.length()) {
                break;
            }
            tmp = tmp.substring(index);
        }

        return chunks;
    }

    /**
     * Find the first space before position w or if there is none after w.
     *
     * @param s String
     * @param width Width
     * @return index of best whitespace or <code>-1</code> if no whitespace was
     *         found.
     */
    public static int findSplitpoint(String s, int width) {
        // the newline (or EOS) is the fallback split position.
        int in = s.indexOf(NEWLINE);
        if (in < 0) {
            in = s.length();
        }
        // Good enough?
        if (in < width) {
            return in;
        }
        // otherwise, search for whitespace
        int iw = s.lastIndexOf(' ', width);
        // good whitespace found?
        if (iw >= 0 && iw < width) {
            return iw;
        }
        // sub-optimal splitpoint - retry AFTER the given position
        int bp = nextPosition(s.indexOf(' ', width), s.indexOf(NEWLINE, width));
        if (bp >= 0) {
            return bp;
        }
        // even worse - can't split!
        return s.length();
    }

    /**
     * Helper that is similar to {@code Math.min(a,b)}, except that negative
     * values are considered "invalid".
     *
     * @param a String position
     * @param b String position
     * @return {@code Math.min(a,b)} if {@code a >= 0} and {@code b >= 0},
     *         otherwise whichever is positive.
     */
    private static int nextPosition(int a, int b) {
        if (a < 0) {
            return b;
        }
        if (b < 0) {
            return a;
        }
        return Math.min(a, b);
    }
}

Related

  1. splitArguments(String string)
  2. splitArguments(String string)
  3. splitArray(byte[] src, int size)
  4. splitArray(String[] srcArr, int start, int end)
  5. splitAt(final String input, final String seperator)
  6. splitAttributes(final String dname)
  7. splitAuthors(String source)
  8. splitBreaks(String text)
  9. splitByTypeAndName(final String s)