Java String Repeat repeat(String str, int repeat)

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

Description

repeat

License

Apache License

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.List;

public class Main {
    private static String repeat(String str, int repeat) {
        StringBuilder buffer = new StringBuilder(repeat * str.length());

        for (int i = 0; i < repeat; i++) {
            buffer.append(str);//from w  ww. j  a v  a2 s . c o  m
        }

        return buffer.toString();
    }

    private void append(StringBuilder sb, String description, int indent) {
        for (String line : toLines(description, indent, 2, 100)) {
            sb.append(line).append('\n');
        }
    }

    private static List<String> toLines(String text, int indent,
            int indentSize, int lineLength) {
        List<String> lines = new ArrayList<String>();

        String ind = repeat("\t", indent);

        String[] plainLines = text.split("(\r\n)|(\r)|(\n)");

        for (String plainLine : plainLines) {
            toLines(lines, ind + plainLine, indentSize, lineLength);
        }

        return lines;
    }

    private static void toLines(List<String> lines, String line,
            int indentSize, int lineLength) {
        int lineIndent = getIndentLevel(line);
        StringBuilder buf = new StringBuilder(256);

        String[] tokens = line.split(" +");

        for (String token : tokens) {
            if (buf.length() > 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());
    }

    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 cnt)
  2. repeat(String s, int times)
  3. repeat(String s, int times)
  4. repeat(String s, int times)
  5. repeat(String str, int count)
  6. repeat(String str, int repeat)
  7. repeat(String string, int n)
  8. repeat(String string, int number)
  9. repeat(String string, int times)