This is my previous question子不在此行<code>lastDot</code>越來越問題工作
在我上面的問題的回答return id.substring(0, lastDot - 1);
。
如果我有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);
}
閱讀子字符串的文檔。如果你注意,你會看到你的誤解。不要減去。閱讀文檔和使用代碼進行實驗要比在SO上提出要快。自問你已經16分鐘了。這在調試器中可以很容易地修復一兩分鐘。每個問題都不適合在這裏詢問。 – duffymo
在你的例子中, 子字符串似乎是錯誤的方法。 考慮在'。'上使用拆分和拆分。 – DwB