有条件地删除行后缀并添加前缀

Conditionally delete line suffix and add prefix

我有 4 行:

812.12 135.14 646.17 1
812.12 135.14 646.18 1
812.12 135.14 646.19 2
812.12 135.14 646.20 2

我想删除每行的最后一个字符。此外,如果最后一个字符与上一行不同,则会在前面添加 S。例如,给定上面的输入,第三行的最后一个字符是 2,它不同于终止第二行的 1。所以前面会加上S如下图。

期望的输出:

812.12 135.14 646.17 
812.12 135.14 646.18
S 812.12 135.14 646.19 
812.12 135.14 646.20 

我有这段代码可以开始:

while(lines[0].charAt(lines[0].length()-1)==lines[1].charAt(lines[1].length()-1)){
    continue;
}

此代码有效:

public static void main(String[] args) {
    String[] lines = new String[4];
    lines[0] = "812.12 135.14 646.17 1";
    lines[1] = "812.12 135.14 646.18 1";
    lines[2] = "812.12 135.14 646.19 2";
    lines[3] = "812.12 135.14 646.20 2";

    // first one is done separately to extract last character
    // for comparisons (for next iterations)
    char lastChar = lines[0].charAt(lines[0].length()-1);
    lines[0] = lines[0].substring(0, lines[0].length()-2);

    // start from index 1 since index 0 was already processed
    for(int i=1;i<lines.length; i++){
        // get last character
        char tmp = lines[i].charAt(lines[i].length()-1);

        // remove last character
        lines[i] = lines[i].substring(0, lines[i].length()-2);

        // if last character from this line isn't the same like
        // the last character from previous line, add S
        if(tmp != lastChar)
            lines[i] = "S " + lines[i];

        // update lastChar for comparison
        lastChar = tmp;
    }

    for(int i=0;i<lines.length;i++)
        System.out.println(lines[i]);
}

输出:

812.12 135.14 646.17
812.12 135.14 646.18
S 812.12 135.14 646.19
812.12 135.14 646.20

或者如果您希望有一个接受 String[] 并对其进行修改的函数:

static void updateLines(String[] lines){
    // first one is done separately to extract last character
    // for comparisons (for next iterations)
    char lastChar = lines[0].charAt(lines[0].length()-1);
    lines[0] = lines[0].substring(0, lines[0].length()-2);

    // start from index 1 since index 0 was already processed
    for(int i=1;i<lines.length; i++){
        // get last character
        char tmp = lines[i].charAt(lines[i].length()-1);

        // remove last character
        lines[i] = lines[i].substring(0, lines[i].length()-2);

        // if last character from this line isn't the same like
        // the last character from previous line, add S
        if(tmp != lastChar)
            lines[i] = "S " + lines[i];

        // update lastChar for comparison
        lastChar = tmp;
    }
}


public static void main(String[] args) {
    String[] lines = new String[4];
    lines[0] = "812.12 135.14 646.17 1";
    lines[1] = "812.12 135.14 646.18 1";
    lines[2] = "812.12 135.14 646.19 2";
    lines[3] = "812.12 135.14 646.20 2";

    updateLines(lines);

    for(int i=0;i<lines.length;i++)
        System.out.println(lines[i]);
}

输出:

812.12 135.14 646.17
812.12 135.14 646.18
S 812.12 135.14 646.19
812.12 135.14 646.20

尝试:

String[] lines = new String[]{
        "812.12 135.14 646.17 1", 
        "812.12 135.14 646.18 1", 
        "812.12 135.14 646.19 2", 
        "812.12 135.14 646.20 2"};

System.out.println(lines[0].substring(0, lines[0].length() - 1));
for(int i=1; i < lines.length; i++){
    if(lines[i-1].charAt(lines[i-1].length() - 1) != lines[i].charAt(lines[i].length() - 1)){
        System.out.println("S " + lines[i].substring(0, lines[i].length() - 1));
    } else {
        System.out.println(lines[i].substring(0, lines[i].length() - 1));
    }
}

输出:

812.12 135.14 646.17 
812.12 135.14 646.18 
S 812.12 135.14 646.19 
812.12 135.14 646.20