Java String Indent Format indent(String original, int spaces)

Here you can find the source of indent(String original, int spaces)

Description

indent

License

Open Source License

Declaration

public static String indent(String original, int spaces) 

Method Source Code

//package com.java2s;
/*/*from  ww  w.j  a  va2 s  .c  o m*/
 * Copyright (c) 2016 Vivid Solutions.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

public class Main {
    public static String indent(String original, int spaces) {
        String indent = stringOfChar(' ', spaces);
        String indented = indent + original;

        indented = replaceAll(indented, "\r\n", "<<<<.CRLF.>>>>");
        indented = replaceAll(indented, "\r", "<<<<.CR.>>>>");
        indented = replaceAll(indented, "\n", "<<<<.LF.>>>>");

        indented = replaceAll(indented, "<<<<.CRLF.>>>>", "\r\n" + indent);
        indented = replaceAll(indented, "<<<<.CR.>>>>", "\r" + indent);
        indented = replaceAll(indented, "<<<<.LF.>>>>", "\n" + indent);
        return indented;
    }

    /**
     *  Returns a String of the given length consisting entirely of the given
     *  character
     */
    public static String stringOfChar(char ch, int count) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < count; i++) {
            buf.append(ch);
        }
        return buf.toString();
    }

    /**
     *  Returns original with all occurrences of oldSubstring replaced by
     *  newSubstring
     */
    public static String replaceAll(String original, String oldSubstring, String newSubstring) {
        return replace(original, oldSubstring, newSubstring, true);
    }

    /**
     *  Returns a string with all occurrences of oldChar replaced by newStr
     */
    public static String replace(String str, char oldChar, String newStr) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (ch == oldChar) {
                buf.append(newStr);
            } else {
                buf.append(ch);
            }
        }
        return buf.toString();
    }

    /**
     *  Replaces all instances of the String o with the String n in the
     *  StringBuffer orig if all is true, or only the first instance if all is
     *  false. Posted by Steve Chapel <schapel@breakthr.com> on UseNet
     */
    public static void replace(StringBuffer orig, String o, String n, boolean all) {
        if (orig == null || o == null || o.length() == 0 || n == null) {
            throw new IllegalArgumentException("Null or zero-length String");
        }
        int i = 0;
        while (i + o.length() <= orig.length()) {
            if (orig.substring(i, i + o.length()).equals(o)) {
                orig.replace(i, i + o.length(), n);
                if (!all) {
                    break;
                } else {
                    i += n.length();
                }
            } else {
                i++;
            }
        }
    }

    /**
     *  Returns original with occurrences of oldSubstring replaced by
     *  newSubstring. Set all to true to replace all occurrences, or false to
     *  replace the first occurrence only.
     */
    public static String replace(String original, String oldSubstring, String newSubstring, boolean all) {
        StringBuffer b = new StringBuffer(original);
        replace(b, oldSubstring, newSubstring, all);
        return b.toString();
    }
}

Related

  1. indent(String in)
  2. indent(String input, int depth, int start)
  3. indent(String lines, String indentation)
  4. indent(String message, int indent)
  5. indent(String msg, int size)
  6. indent(String s)
  7. indent(String s)
  8. indent(String s)
  9. indent(String s)