substring不起作用

时间:2017-08-12 13:27:26

标签: java string

This is my previous question

在上面的问题中,return id.substring(0, lastDot - 1);这一行中的答案lastDot正在遇到问题。

如果我的ID为1.1,则上面的行lastDot值为1,子字符串的格式为""

我该如何避免这个问题。

private String getParentId(String id) {
    int lastDot = id.lastIndexOf(".");
    if (lastDot == -1) {
        return null;
    }
    return id.substring(0, lastDot - 1);
}

0 个答案:

没有答案