替换字符串中字符的最后一次出现 [英] Replace Last Occurrence of a character in a string

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

问题描述

我有一个这样的字符串

"Position, fix, dial"

我想用转义双引号(")替换最后一个双引号(")

I want to replace the last double quote(") with escape double quote(")

字符串的结果是

"Position, fix, dial"

我该怎么做.我知道替换第一次出现的字符串.但不知道如何替换最后一次出现的字符串

How can I do this. I am aware of replacing the first occurrence of the string. but don't know how to replace the last occurrence of a string

推荐答案

String str = ""Position, fix, dial"";
int ind = str.lastIndexOf(""");
if( ind>=0 )
    str = new StringBuilder(str).replace(ind, ind+1,"\"").toString();
System.out.println(str);

更新

 if( ind>=0 )
    str = new StringBuilder(str.length()+1)
                .append(str, 0, ind)
                .append('\')
                .append(str, ind, str.length())
                .toString();

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

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