Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8986175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:29:18+00:00 2026-06-15T21:29:18+00:00

I would like to insert an extra character (or a new string) at a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:29:20+00:00Added an answer on June 15, 2026 at 9:29 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to insert a new DataGridViewRow into my DataGridView at a specific
I would like to insert notes on the fixes to be done in specific
I would like to insert a little extra space before each <h2> tag I
I would like to insert the current Subversion revision number (as reported by svnversion
I would like to insert dates in an emacs buffer like I do in
I would like to insert the output of multiple calculations into my text document:
In a part of my website I would like to insert some images and
I have a little problem where I would like to insert a svn diff
I have a JPA project and I would like to insert some initial data
I have a Datatable in my C# program that I would like to INSERT

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.