替换最后一次出现的子串 [英] Replace last occurrence of substring

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

问题描述

给定这个字符串:

.ABC,.123,.FGH,.QDG

.ABC,.123,.FGH,.QDG

如何最有效地使用 PL/SQL 进行输出(注意,和"):

How can I most-efficiently use PL/SQL to output (note the ", and"):

abc、123、fgh 和 qdg

abc, 123, fgh, and qdg

到目前为止,我得到了 LOWER(REPLACE('.ABC,.123,.FGH,.QDG', '.', ' ')),结果是

So far I've got LOWER(REPLACE('.ABC,.123,.FGH,.QDG', '.', ' ')), which yields

abc, 123, fgh, qdg

abc, 123, fgh, qdg

如何在fgh,"之后但在qdg"(fgh, and qdg")之前得到and"?我必须使用 INSTR()SUBSTR(),还是有一个函数可以用和"替换最后一个空格?

How do I get that "and" after "fgh,", but before "qdg" ("fgh, and qdg")? Must I use INSTR() and SUBSTR(), or is there a function that can just replace the last space with " and "?

推荐答案

建立在你已经使用的基础上......使用 regexp_replaceinstr

Building on what you used already... use regexp_replace with instr

with cte as(
select LOWER(REPLACE('.ABC,.123,.FGH,.QDG', '.', ' ')) as val from dual)
select regexp_replace(val,' ',' and ',instr(val ,' ',-1))
from cte;

regexp_replace(val,', ',' and ',instr(val ,', ',-1)) 如果你也想去掉最后一个逗号.

or regexp_replace(val,', ',' and ',instr(val ,', ',-1)) if you want to get rid of the last comma too.

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

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