按逗号分割字符串,并将所有分割字符串居中

时间:2019-06-19 08:19:46

标签: java string

尝试用逗号分割字符串,然后将所有已分割的字符串居中时,我遇到问题。

我尝试使用https://stackoverflow.com/a/8368917此处的答案来拆分字符串

public static String split (String str) {
    StringBuilder stringBuilder = new StringBuilder(str);
    String[] strings = stringBuilder.toString().split(",");

    for(int i = 0; i < strings.length; i++){
        strings[i] = strings[i].trim();
        strings[i] += ",\n";
    }

    stringBuilder = new StringBuilder();
    for (String string : strings) {
        stringBuilder.append(string);
    }

    // Remove last separator
    if (stringBuilder.length() > 0) {
        stringBuilder.setLength(stringBuilder.length() - ",\n".length());
    }

    return stringBuilder.toString();
}

这里是中心字符串(忘记了来源)

public static String center (String str, int length) {
    if (str.length() > length) {
        return str.substring(0, length);
    } else if (str.length() == length) {
        return str;
    } else {
        int leftPadding = (length - str.length()) / 2;
        StringBuilder leftBuilder = new StringBuilder();
        for (int i = 0; i < leftPadding; i++) {
            leftBuilder.append(" ");
        }

        int rightPadding = length - str.length() - leftPadding;
        StringBuilder rightBuilder = new StringBuilder();
        for (int i = 0; i < rightPadding; i++) {
            rightBuilder.append(" ");
        }

        return leftBuilder.toString() + str + rightBuilder.toString();
    }
}

这是我想做的

String STR;
STR = STR + String.format("%1$48s", center(split(Address), 48)) + "\n";
System.out.println(STR);

预期结果

        Street Road Patta 55,
         053888 Alur Satta,
              Keidaj

实际结果

   Street Road Patta 55,
053888 Alur Satta,
Keidaj

1 个答案:

答案 0 :(得分:1)

您将字符串分割成一个字符串。 您应该将字符串分成N个字符串(行),然后将每行居中:

import java.util.stream.Stream;

public class Center {

    public Center() {
    }

    public static String center (String str, int length) {
        if (str.length() > length) {
            return str.substring(0, length);
        } else if (str.length() == length) {
            return str;
        } else {
            int leftPadding = (length - str.length()) / 2;
            StringBuilder leftBuilder = new StringBuilder();
            for (int i = 0; i < leftPadding; i++) {
                leftBuilder.append(" ");
            }

            int rightPadding = length - str.length() - leftPadding;
            StringBuilder rightBuilder = new StringBuilder();
            for (int i = 0; i < rightPadding; i++) {
                rightBuilder.append(" ");
            }

            return leftBuilder.toString() + str + rightBuilder.toString();
        }
    }

    public static void main(String[] args) {

        String input = "Street Road Patta 55,053888 Alur Satta,Keidaj";

        Stream.of(input.split(",")).map(line -> center(line, 48)).forEach(System.out::println);

    }

}

输出:

              Street Road Patta 55              
               053888 Alur Satta                
                     Keidaj                     

相同但没有lambdas

public static void main(String[] args) {

    String input = "Street Road Patta 55,053888 Alur Satta,Keidaj";

    final String[] lines = input.split(",");
    for(String line : lines) {
        System.out.println(center(line, 48));
    }

}

HTH!