Here you can find the source of formatSampleText(final List
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 |
public static String formatSampleText(final List<String> lines, final int maxTrim)
//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; } }