Java List Trim trimLeadingWhitespaces(List lines)

Here you can find the source of trimLeadingWhitespaces(List lines)

Description

Trims leading whitespaces.

License

Apache License

Parameter

Parameter Description
lines the source lines

Return

the trimmed lines

Declaration

public static List<String> trimLeadingWhitespaces(List<String> lines) 

Method Source Code

//package com.java2s;
/**//  ww  w. j a va  2 s  . c  o m
 * Copyright 2014 ashigeru.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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.ArrayList;
import java.util.List;

public class Main {
    /**
     * Trims leading whitespaces.
     * @param lines the source lines
     * @return the trimmed lines
     */
    public static List<String> trimLeadingWhitespaces(List<String> lines) {
        if (lines.isEmpty()) {
            return lines;
        }
        String lead = null;
        for (int i = 0, n = lines.size(); i < n; i++) {
            String line = lines.get(i);
            if (line.isEmpty() == false) {
                if (lead == null) {
                    lead = getLeadingWhitespaces(line);
                } else {
                    lead = getCommonPrefix(lead, getLeadingWhitespaces(line));
                }
            }
        }
        if (lead == null || lead.isEmpty()) {
            return lines;
        }
        List<String> results = new ArrayList<String>();
        for (String line : lines) {
            if (line.length() >= lead.length()) {
                results.add(line.substring(lead.length()));
            }
        }
        return results;
    }

    private static String getLeadingWhitespaces(String string) {
        StringBuilder buf = new StringBuilder();
        for (int i = 0, n = string.length(); i < n; i++) {
            char c = string.charAt(i);
            if (Character.isWhitespace(c) == false) {
                break;
            }
            buf.append(c);
        }
        return buf.toString();
    }

    private static String getCommonPrefix(String a, String b) {
        StringBuilder common = new StringBuilder();
        for (int i = 0, n = Math.min(a.length(), b.length()); i < n; i++) {
            if (a.charAt(i) == b.charAt(i)) {
                common.append(a.charAt(i));
            } else {
                break;
            }
        }
        return common.toString();
    }
}

Related

  1. trim(List strs)
  2. trimElements(List list)
  3. trimEndDigital(List strs)
  4. trimIfLastIndex(List suggestedList, String suggestedWords, int i)
  5. trimKeyFormat(String s3KeyFormat, List disallowedKeys)
  6. trimList(List list)
  7. trimList(List source)
  8. trimListValues(List original)
  9. trimSequence(int m, List sequence)