替换字符串的最后一部分
问题描述:
我想用 )
替换最后一个 ,
字符串.
I want to replace the last String which is a ,
with )
.
假设字符串是:
插入对偶(姓名、日期、
Insert into dual (name,date,
要转换为:
插入双(名称,日期)
答
以下代码应将最后出现的 ','
替换为 ')'
.
The following code should replace the last occurrence of a ','
with a ')'
.
StringBuilder b = new StringBuilder(yourString);
b.replace(yourString.lastIndexOf(","), yourString.lastIndexOf(",") + 1, ")" );
yourString = b.toString();
注意 如果 String
不包含 ','
,这将抛出异常.
Note This will throw Exceptions if the String
doesn't contain a ','
.