如何将字符串对齐到右侧

时间:2016-06-15 04:49:54

标签: java

所以我有这个简单的字符串:

dgj gjnw - gwgt: 434.572
jfdhdr - Report: 342.203
dsdgs ryr4y4: 26.724
etet erte neytwe eyqw: 1338.843
etyer eyey: 16.742
efaewf were: 122.325

我想按照正常的顺序将此顺序排列为values,例如:

dgj gjnw - gwgt:        434.572
jfdhdr - Report:        342.203
dsdgs ryr4y4:           26.724
etet erte neytwe eyqw:  1338.843
etyer eyey:             16.742
efaewf were:            122.325

4 个答案:

答案 0 :(得分:0)

您可以在\t字符出现后给出空格(:)。请尝试以下代码。

String s = "dgj gjnw - gwgt: 434.572";
    String s0=" ";
    for(int i=0;i<s.length();i++){
        System.out.print(s.charAt(i));
        if(s.charAt(i) == ':'){
            s0 = s.substring(i+1);
            break;
        }
    }   
    System.out.println("\t"+s0);  

我将:之后的字符作为另一个子字符串,并在最后提供\t后打印。

答案 1 :(得分:0)

尝试以下已在IntelliJ上测试过的解决方案,似乎正在运行。首先,我迭代每一行以找到最长字符串的长度,该值将确定需要将多少空白填充添加到输出中的每一行。接下来我迭代第二次,应用适当的填充量。在第二次迭代中,我用冒号分割,将标签与值分开。

String lineInput= "dgj gjnw - gwgt: 434.572\n" +
                  "jfdhdr - Report: 342.203\n" +
                  "dsdgs ryr4y4: 26.724\n" +
                  "etet erte neytwe eyqw: 1338.843\n" +
                  "etyer eyey: 16.742\n" +
                  "efaewf were: 122.325\n";

String[] lines = lineInput.split("\n");
int maxLength = 0;

// find length of longest line
for (String line : lines) {
    int length = line.replaceAll("\\s\\d+\\.\\d+", "").length();
    if (length > maxLength) {
        maxLength = length;
    }
}

// reformat each line and output to console
for (String line : lines) {
    int length = line.replaceAll("\\s\\d+\\.\\d+", "").length();
    String padding = new String(new char[maxLength-length]).replace("\0", " ");
    String[] parts = line.split(":");
    String output = parts[0] + ":" + padding + parts[1];
     System.out.println(output);
}

<强>输出:

dgj gjnw - gwgt:       434.572
jfdhdr - Report:       342.203
dsdgs ryr4y4:          26.724
etet erte neytwe eyqw: 1338.843
etyer eyey:            16.742
efaewf were:           122.325

答案 2 :(得分:0)

public static void main(String[] args) {
        String a[] = { "dgj gjnw - gwgt: 434.572", "jfdhdr - Report: 342.203", "dsdgs ryr4y4: 26.724", "etet erte neytwe eyqw: 1338.843", "etyer eyey: 16.742",
                "efaewf were: 122.325" };
        for (int i = 0; i < a.length; i++) {
            if (a[i].split(": ")[0].length() < 15)//checking for length so that the data is perfectly aligned as you wanted.
                System.out.println(a[i].split(":")[0] + ":\t\t" + a[i].split(":")[1]);
            else
                System.out.println(a[i].split(":")[0] + ":\t" + a[i].split(":")[1]);

        }
}

上面的代码产生以下输出:

dgj gjnw - gwgt:         434.572
jfdhdr - Report:         342.203
dsdgs ryr4y4:            26.724
etet erte neytwe eyqw:   1338.843
etyer eyey:              16.742
efaewf were:             122.325

答案 3 :(得分:0)

您可以从此代码段开始。

// create a list of the input strings
List<String> strings = Arrays.asList("dgj gjnw - gwgt: 434.572",
        "jfdhdr - Report: 342.203",
        "dsdgs ryr4y4: 26.724",
        "etet erte neytwe eyqw: 1338.843",
        "etyer eyey: 16.742",
        "efaewf were: 122.325");
// find the maximum length of the left part (it assume there is no additional
// colon in the left part
int maxColonPos = strings.stream().mapToInt(s->s.indexOf(':')).max().getAsInt();
// construct a output format string
String format = "%-" + maxColonPos + "s: %s%n";
// stream over all strings
strings.stream()
        // split them at the position of the colon (it assume that there are
        // always two colons
        .map((s) -> s.split(":", 2)) 
        // print the splitted values using the output format
        .forEach((p) -> {System.out.printf(format, p[0], p[1]);
});

输出

dgj gjnw - gwgt      :  434.572
jfdhdr - Report      :  342.203
dsdgs ryr4y4         :  26.724
etet erte neytwe eyqw:  1338.843
etyer eyey           :  16.742
efaewf were          :  122.325