文本格式化程序仅使用字符串

时间:2013-11-15 01:40:54

标签: java string

对于我们的java类,我们必须使用L allignCenteredR allign.

格式化文本

我已经想出如何将字符串保存到一行,但我不知道如何重置该行。

任何帮助都会很棒!

public static String word(String text, int width) {

    String word = "";
    String all = "";
    String row = "";
    String newRow = "";
    int textlen = text.length();

    for (int i = 0; i < text.length(); i++) {
        char current = text.charAt(i);
        // works gives a word
        do {
            if (text.charAt(i) != ' ') {

                word += text.charAt(i);
            }
            i++;

        } while (text.charAt(i) != ' ' && i < textlen);
        // till here
        word += " ";
        // System.out.println("this is word: " +word);

        while (row.length() < width) {
            if (word.length() < width
                    && (row.length() + word.length()) < width) {
                row += word;
                if ((width - (row.length() + word.length())) < 3) {
                    System.out.println("here is row: " + row);
                }

            }
            if (word.length() > width
                    && (row.length() + word.length()) > width) {

            }
            word = "";
        }
        row = "";
    }

    return word;
}

1 个答案:

答案 0 :(得分:0)

如果要将String变量重置为空字符串,只需将其分配给空字符串:

row = "";