Java String Repeat repeat(String str, int repeat)

Here you can find the source of repeat(String str, int repeat)

Description

Repeat a String n times to form a new string.

License

Apache License

Parameter

Parameter Description
str String to repeat
repeat number of times to repeat str

Exception

Parameter Description
NegativeArraySizeException if <code>repeat < 0</code>
NullPointerException if str is <code>null</code>

Return

String with repeated String

Declaration

private static String repeat(String str, int repeat) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Main {
    /**/*from   www  .jav a 2 s.c o m*/
     * The maximum length of a display line.
     *
     * @parameter expression="${lineLength}" default-value="80"
     */
    private int lineLength;
    /**
     * The number of spaces per indentation level.
     *
     * @parameter expression="${indentSize}" default-value="2"
     */
    private int indentSize;

    /**
     * <p>Repeat a String <code>n</code> times to form a new string.</p>
     *
     * @param str String to repeat
     * @param repeat number of times to repeat str
     * @return String with repeated String
     * @throws NegativeArraySizeException if <code>repeat < 0</code>
     * @throws NullPointerException if str is <code>null</code>
     */
    private static String repeat(String str, int repeat) {
        StringBuffer buffer = new StringBuffer(repeat * str.length());

        for (int i = 0; i < repeat; i++) {
            buffer.append(str);
        }

        return buffer.toString();
    }

    private void append(StringBuffer sb, String description, int indent) {
        for (Iterator it = toLines(description, indent).iterator(); it
                .hasNext();) {
            sb.append(it.next().toString()).append('\n');
        }
    }

    /**
     * Splits the specified text into lines of convenient display length.
     *
     * @param text The text to split into lines, must not be <code>null</code>.
     * @param indent The base indentation level of each line, must not be negative.
     * @return The sequence of display lines, never <code>null</code>.
     */
    private List toLines(String text, int indent) {
        List lines = new ArrayList();

        String ind = repeat("\t", indent);
        String[] plainLines = text.split("(\r\n)|(\r)|(\n)");
        for (int i = 0; i < plainLines.length; i++) {
            toLines(lines, ind + plainLines[i]);
        }

        return lines;
    }

    /**
     * Adds the specified line to the output sequence, performing line wrapping if necessary.
     *
     * @param lines The sequence of display lines, must not be <code>null</code>.
     * @param line The line to add, must not be <code>null</code>.
     */
    private void toLines(List lines, String line) {
        int lineIndent = getIndentLevel(line);
        StringBuffer buf = new StringBuffer(256);
        String[] tokens = line.split(" +");
        for (int i = 0; i < tokens.length; i++) {
            String token = tokens[i];
            if (i > 0) {
                if (buf.length() + token.length() >= lineLength) {
                    lines.add(buf.toString());
                    buf.setLength(0);
                    buf.append(repeat(" ", lineIndent * indentSize));
                } else {
                    buf.append(' ');
                }
            }
            for (int j = 0; j < token.length(); j++) {
                char c = token.charAt(j);
                if (c == '\t') {
                    buf.append(repeat(" ", indentSize - buf.length()
                            % indentSize));
                } else if (c == '\u00A0') {
                    buf.append(' ');
                } else {
                    buf.append(c);
                }
            }
        }
        lines.add(buf.toString());
    }

    /**
     * Gets the indentation level of the specified line.
     *
     * @param line The line whose indentation level should be retrieved, must not be <code>null</code>.
     * @return The indentation level of the line.
     */
    private static int getIndentLevel(String line) {
        int level = 0;
        for (int i = 0; i < line.length() && line.charAt(i) == '\t'; i++) {
            level++;
        }
        for (int i = level + 1; i <= level + 4 && i < line.length(); i++) {
            if (line.charAt(i) == '\t') {
                level++;
                break;
            }
        }
        return level;
    }
}

Related

  1. repeat(String s, int times)
  2. repeat(String s, int times)
  3. repeat(String s, int times)
  4. repeat(String str, int count)
  5. repeat(String str, int repeat)
  6. repeat(String string, int n)
  7. repeat(String string, int number)
  8. repeat(String string, int times)
  9. repeat(String string, int times)