Java List Trim formatSampleText(final List lines, final int maxTrim)

Here you can find the source of formatSampleText(final List lines, final int maxTrim)

Description

Formats a section of sample text by removing leading whitespace characters up to a given maximum and by flattening lines into a single string.

License

Open Source License

Parameter

Parameter Description
lines list where each element represents an individual line of sample text
maxTrim maximum number of leading whitespace characters to trim from each line

Return

formatted sample text string

Declaration

public static String formatSampleText(final List<String> lines, final int maxTrim) 

Method Source Code

//package com.java2s;
/*//w w  w  .j  av a 2s  . c  om
 * Copyright 2013-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License").
 * You may not use this file except in compliance with the License.
 * A copy of the License is located at
 *
 *  http://aws.amazon.com/apache2.0
 *
 * or in the "license" file accompanying this file. This file is distributed
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */

import java.util.List;

public class Main {
    /**
     * Formats a section of sample text by removing leading whitespace
     * characters up to a given maximum and by flattening lines into a single
     * string.
     *
     * @param lines
     *            list where each element represents an individual line of
     *            sample text
     * @param maxTrim
     *            maximum number of leading whitespace characters to trim from
     *            each line
     * @return formatted sample text string
     */
    public static String formatSampleText(final List<String> lines, final int maxTrim) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < lines.size(); i++) {
            sb.append(trimLeadingWhitespace(lines.get(i), maxTrim));
            if (i + 1 < lines.size()) {
                sb.append("\n");
            }
        }
        return sb.toString();
    }

    /**
     * Trims up to a certain number of leading whitespace characters from a
     * string.
     *
     * @param str
     *            string to process
     * @param max
     *            maximum number of whitespace characters to trim
     * @return trimmed String
     */
    public static String trimLeadingWhitespace(final String str, final int max) {
        int pos = Math.min(getLeadingWhitespace(str), max);
        if (pos > 0) {
            return str.substring(pos);
        } else {
            return str;
        }
    }

    /**
     * Returns the number of leading whitespace characters in a given string.
     *
     * @param str
     *            string to check
     * @return number of leading whitespace characters
     */
    public static int getLeadingWhitespace(final String str) {
        int pos = 0;
        while ((pos < str.length()) && (str.charAt(pos) == ' ')) {
            pos++;
        }
        return pos;
    }
}

Related

  1. addTrimmed(StringBuffer stringbuffer, java.util.List list, int i)
  2. addTrimString(List aStringList, String aString)
  3. getTrimmedList(List listA)
  4. splitAndTrimCSVToList(String csv)
  5. splitAsList(String source, char delimiter, boolean trim)
  6. stringToListTrim(String str, char delimiter)