Kin*_*lan 1 java arrays string char
我试图从字符串中删除最后一个字符,如果它是/.我正在使用字符串数组temp[]来存储字符串.
这是我的代码:
char ch = ' ';
for (int st = 0; st < temp.length; st++)
{
ch = temp[st].charAt(temp[st].length()-1);
if (ch == '/')
temp[st] = temp[st].substring(0, temp[st].length()-1);
result2.append(temp[st]);
}
Run Code Online (Sandbox Code Playgroud)
但我得到了
StringIndexOutOfBoundsException -1
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
aio*_*obe 21
删除最后一个字符,如果它是/ java
str = str.replaceAll("/$", "");
Run Code Online (Sandbox Code Playgroud)
Gui*_*let 12
如果你只想删除一个尾随'/',这应该这样做:
if (str.endsWith("/"))
return str.substring(0,str.length()-1);
else
return str;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6289 次 |
| 最近记录: |