替换字符串的最后一部分 [英] Replace the last part of a string

查看:43
本文介绍了替换字符串的最后一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 ) 替换最后一个 , 字符串.

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 ','.

这篇关于替换字符串的最后一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆