在字符串中的特定位置替换字符 [英] Replace character at certain location within string

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

问题描述

给出特定的字符串,例如 s = tesX123 ,如何在特定位置替换特定字符?



在此示例中,位置 4 的字符应更改为 t 。 / p>

是否存在 setChar(s,4, t)样式的方法,该方法会导致 test123

解决方案

尝试 substr()

  substr(s,4,4)<- t 
> s
#[1] test123


Given a certain string, e.g., s = "tesX123", how can I replace a certain character at a certain location?

In this example, the character at position 4 should be changed to "t".

Does a method exist in the style of setChar(s, 4, "t") which would result in test123?

解决方案

Try substr()

substr(s, 4, 4) <- "t"
> s
#[1] "test123"

这篇关于在字符串中的特定位置替换字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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