Java中的StringBuilder与rtrim等效

时间:2018-06-23 13:43:53

标签: java string replace stringbuilder

我正在尝试从右侧修剪public static int[] solution(int N, int[] A) { int[] counters = new int[N]; int currMax = 0; int currMin = 0; for (int i = 0; i < A.length; i++) { if (A[i] <= N) { counters[A[i] - 1] = Math.max(currMin, counters[A[i] - 1]); counters[A[i] - 1]++; currMax = Math.max(currMax, counters[A[i] - 1]); } else if (A[i] == N + 1) { currMin = currMax; } } for (int i = 0; i < counters.length; i++) { counters[i] = Math.max(counters[i], currMin); } return counters; } ,如下所示:

StringBuilder

上面的函数可以很好地与trimStr一起使用,但是我想将trimStr作为字符串传递。是否有相同的库(类似于StringUtils)?

3 个答案:

答案 0 :(得分:4)

您可以使用replaceAll或仅使用regex替换replaceFirst:

String str = "  some string  ";
str = str.replaceAll("\\s*$", "");//output is "  some string"

编辑

如果您使用的是StringBuilder,则可以使用:

private static StringBuilder rtrim(StringBuilder str, String trimStr) {
    return new StringBuilder(
            str.toString().replaceFirst("(" + Pattern.quote(trimStr) + ")+$", "")
    );
}

一些输入和输出

"  some string   "    + " "    ==> "  some string"
"  some stringaaa"    + "a"    ==> "  some string"
"  some stringabab"   + "ab"   ==> "  some string"

左装饰(lTrim)

对于左侧修饰,您只需将正则表达式更改为str.toString().replaceFirst("^(" + Pattern.quote(trimStr) + ")+", "")

private static StringBuilder rtrim(StringBuilder str, String trimStr) {
    return new StringBuilder(
            str.toString().replaceFirst("^(" + Pattern.quote(trimStr) + ")+", "")
    );
}

没有正则表达式的另一种解决方案

右修剪(rTrim)

public StringBuilder rTrim(StringBuilder str, String trimStr) {
    if (str != null && trimStr != null) {
        int trimLen = trimStr.length();
        int strLen = str.length();
        if (strLen != 0 && trimLen != 0) {
            while (strLen != 0 && str.substring(strLen - trimLen, strLen).equals(trimStr)) {
                str = str.delete(strLen - trimLen, strLen);
                strLen = str.length();
            }
        }
    }
    return str;
}

左装饰(lTrim)

public StringBuilder lTrim(StringBuilder str, String trimStr) {
    if (str != null && trimStr != null) {
        int len = trimStr.length();
        if (str.length() != 0 && len != 0) {
            while (str.length() != 0 && str.substring(0, len).equals(trimStr)) {
                str = str.delete(0, len);
            }
        }
    }
    return str;
}

答案 1 :(得分:0)

类似的事情可能起作用:

private StringBuilder rtrim(StringBuilder str, String trimStr) {
    if(str.length() == 0 || trimStr.isEmpty()) {
        return str;
    }
    int lastIndexOf = str.lastIndexOf(trimStr);

    if(lastIndexOf >= 0 && lastIndexOf + trimStr.length() == str.length()) {
        str.setLength(lastIndexOf);
    }
    return str;
}

这将从提供​​的trimStr的末尾删除str。 直接在lastIndexOf上使用StringBuilder可以避免将所有内容都转换为字符串。

如果要在trimStr的末尾删除所有出现的str,可以执行以下操作:

private StringBuilder rtrim(StringBuilder str, String trimStr) {
    if(str.length() == 0 || trimStr.isEmpty()) {
        return str;
    }
    int lastIndexOf;
    while((lastIndexOf = str.lastIndexOf(trimStr)) >= 0 && lastIndexOf + trimStr.length() == str.length()) {
        str.setLength(lastIndexOf);
    }
    return str;
}

答案 2 :(得分:0)

如前所述,您可以使用Apache StringUtils库:

private static StringBuilder rTrim(StringBuilder sb, String stripChars) {
    return new StringBuilder(StringUtils.stripEnd(sb.toString(), stripChars));
}

private static StringBuilder lTrim(StringBuilder sb, String stripChars) {
    return new StringBuilder(StringUtils.stripStart(sb.toString(), stripChars));
}