Java String Indent Format indentParagraph(String textBefore, String textToModify)

Here you can find the source of indentParagraph(String textBefore, String textToModify)

Description

Return a paragraph who insert spaces at the beginning of lines.

License

Apache License

Parameter

Parameter Description
textBefore String where extract the spaces to insert
textToModify String to modify

Return

indented string

Declaration

public static String indentParagraph(String textBefore, String textToModify) 

Method Source Code

//package com.java2s;
/**/* w w  w.ja  va 2  s.  c o  m*/
 * Copyright (C) 2012 Red Hat, Inc. and/or its affiliates.
 *
 * 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.
 */

public class Main {
    /**
     * Return a paragraph who insert spaces at the beginning of lines.
     * The amount of spaces that will be inserted is equals to the length of
     * all lines, ended with '\n', in the <i>textBefore</i> string, i.e, if
     * textBefore="in\nden\nstr" seven spaces will be inserted to indent the lines
     * of <i>textToModify</i> string.
     *
     * @param textBefore   String where extract the spaces to insert
     * @param textToModify String to modify
     * @return indented string
     * @deprecated Use indentParagraph(String,int) instead
     */
    public static String indentParagraph(String textBefore, String textToModify) {
        int indexU = textBefore.lastIndexOf('\n');
        return indentParagraph(textToModify, indexU + 1);
    }

    /**
     * Insert "indentSize" spaces before every line of textToModify.
     * If indentSize is negative or 0, the string is return unchanged
     *
     * @param textToModify string to indent
     * @param indentSize   number of spaces to insert
     * @return a string whith the lines indented
     */
    public static String indentParagraph(String textToModify, int indentSize) {
        int indexU;
        StringBuffer text;
        String textBegin;
        String textRest;
        String blanks;

        if (indentSize > 0) {
            blanks = repeat(" ", indentSize);
            indexU = textToModify.indexOf('\n');
            if (indexU > -1) {
                textRest = textToModify;
                text = new StringBuffer();
                while (indexU > -1) {
                    textBegin = textRest.substring(0, indexU + 1);
                    textRest = textRest.substring(indexU + 1).trim();
                    text.append(blanks).append(textBegin);
                    indexU = textRest.indexOf('\n');
                }
                text.append(blanks).append(textRest);

                return text.toString();
            } else {
                return textToModify;
            }
        } else {
            return textToModify;
        }
    }

    /**
     * Repeat a String n times.
     *
     * @param str String to repeat
     * @param num number times to repeat
     * @return a String who repeats <I>str</I>, <I>num</I> times.
     */
    public static String repeat(String str, int num) {
        StringBuffer str_ret = new StringBuffer(str.length() * num);

        for (int i = 0; i < num; i++) {
            str_ret.append(str);
        }

        return str_ret.toString();
    }

    /**
     * Returns the position of the first ocurrence of the substring into
     * the stringbuffer, or -1 if does not ocurr.
     * <br>
     * Returns the smaller i such that
     * <p/>
     * buf.substring(i, str.length()).equals(str),
     * <p/>
     * holds, and -1 if no such i exists.
     * <br>
     * Assumes str and buf are non-null.
     *
     * @param str substring to find
     * @param buf buffer to search into
     * @return the position of subStr into buf, or -1 if it does not ocurr
     */
    public static int indexOf(String str, StringBuffer buf) {
        return indexOf(str, 0, buf);
    }

    /**
     * Returns the position of the first ocurrence of the substring into
     * the stringbuffer, starting at fromIndex, or -1 if does not ocurr.
     * <br>
     * Returns the smaller i greater than or equals to fromIndex such that
     * <p/>
     * buf.substring(i, str.length()).equals(str),
     * <p/>
     * holds, and -1 if no such i exists.
     * <p/>
     * Assumes str and buf are non-null.
     *
     * @param str       substring to find
     * @param fromIndex the index to start the search from
     * @param buf       buffer to search into
     * @return the position of subStr into buf, or -1 if it does not ocurr
     */
    public static int indexOf(String str, int fromIndex, StringBuffer buf) {

        fromIndex = Math.max(fromIndex, 0);

        // begin degenerate cases
        if (fromIndex >= buf.length()) {
            if (fromIndex == 0 && str.length() == 0) {
                return 0;
            }
            return -1;
        }
        if (str.length() == 0) {
            return fromIndex;
        }
        // end degenerate cases

        int max = buf.length() - str.length();
        int i = fromIndex;
        while (true) {
            // look for the next occurrence of str.charAt(0)
            while (i <= max && buf.charAt(i) != str.charAt(0)) {
                ++i;
            }
            if (i > max) {
                return -1;
            }
            // once anchored, check the rest
            int j = i + 1, k = 1;
            while (k < str.length() && buf.charAt(j) == str.charAt(k)) {
                ++j;
                ++k;
            }
            if (k == str.length()) {
                return i; // all of them matched
            }
            ++i; // failed, try again
        }
    }

    /**
     * @see #substring(String,int,int)
     * @deprecated Use substring instead
     */
    public static String subString(String str, int beginIndex, int endIndex) {
        return substring(str, beginIndex, endIndex);
    }

    public static String substring(String str, int beginIndex, int endIndex) {
        if (str.length() < endIndex) {
            endIndex = str.length();
        }

        return str.substring(beginIndex, endIndex);
    }
}

Related

  1. indentHtmlSpaces(int count)
  2. indention(int level, int tabSize)
  3. indentLinesAfterNth(final int skip, final int indent, final String str)
  4. indentMultilineValue(String value, StringBuilder tableInfo, int[] columnWidths, boolean printNull)
  5. indentNextLines(String text, String indent)
  6. indentRows(int level, String ss, String indentStr)
  7. indentStr(int level)
  8. indentString(final int indentSpaces)
  9. indentString(int indent)