Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

r - Insert a character at a specific location in a string

I would like to insert an extra character (or a new string) at a specific location in a string. For example, I want to insert d at the fourth location in abcefg to get abcdefg.

Now I am using:

old <- "abcefg"
n <- 4
paste(substr(old, 1, n-1), "d", substr(old, n, nchar(old)), sep = "")

I could write a one-line simple function for this task, but I am just curious if there is an existing function for that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this with regular expressions and gsub.

gsub('^([a-z]{3})([a-z]+)$', '\1d\2', old)
# [1] "abcdefg"

If you want to do this dynamically, you can create the expressions using paste:

letter <- 'd'
lhs <- paste0('^([a-z]{', n-1, '})([a-z]+)$')
rhs <- paste0('\1', letter, '\2')
gsub(lhs, rhs, old)
# [1] "abcdefg"

as per DWin's comment,you may want this to be more general.

gsub('^(.{3})(.*)$', '\1d\2', old)

This way any three characters will match rather than only lower case. DWin also suggests using sub instead of gsub. This way you don't have to worry about the ^ as much since sub will only match the first instance. But I like to be explicit in regular expressions and only move to more general ones as I understand them and find a need for more generality.


as Greg Snow noted, you can use another form of regular expression that looks behind matches:

sub( '(?<=.{3})', 'd', old, perl=TRUE )

and could also build my dynamic gsub above using sprintf rather than paste0:

lhs <- sprintf('^([a-z]{%d})([a-z]+)$', n-1) 

or for his sub regular expression:

lhs <- sprintf('(?<=.{%d})',n-1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...