Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Replacing certain characters in a string

    Dear all

    I want to substitute every second character of a string (e.g. acustomstring) with a character from another string (hello). The final string should look like this: ahuetlmltoing

    Any ideas how this could be done? I used subinstr(s1,s2,s3,n) to match characters but this always replaces the first or more instances of the character and it is problematic.

    Thank you.



  • #2
    Code:
    clear
    set obs 1
    gen whatever = "acustomstring"
    
    tokenize "h e l l o"
    local J : word count h e l l o
    quietly forval j = 1/`J' {
        local j1 = 2 * `j' - 1
        local j2 = 2 * `j' + 1
        replace whatever = substr(whatever, 1, `j1') + "``j''" + substr(whatever, `j2' , .)
    }
    
    list

    Comment


    • #3
      Thank you Nick! But what happens if the other string (hello) has no spaces? Also, what if i want to simply add each character of hello after every second (i.e third) character in acustomstring instead of substituting?
      Last edited by Cynthia Inglesias; 27 Nov 2016, 15:01.

      Comment


      • #4
        Nothing in my solution requires that the string has spaces. Presenting it spaced out is just to split it into characters. Otherwise modifying my solution should seem easy:

        Code:
        quietly forval j = 1/`J' {    
             local j1 = 2 * `j'    
             local j2 = 2 * `j' + 1    
             replace whatever = substr(whatever, 1, `j1') + "``j''" + substr(whatever, `j2' , .)
        }
        Last edited by Nick Cox; 27 Nov 2016, 15:58.

        Comment


        • #5
          Nick, sorry maybe i was not clear. I would like to add each character of hello after every two characters in acustomstring: achusetolmsltroing

          Comment


          • #6
            You just need to play with variations on a theme.

            Code:
            clear
            set obs 1
            gen whatever = "acustomstring"
            
            tokenize h e l l o
            local J : word count h e l l o
            quietly forval j = 1/`J' {
                local j1 = 2 * `j' 
                local j2 = 2 * `j' + 1
                replace whatever = substr(whatever, 1, `j1') + "``j''" + substr(whatever, `j2' , .)
            }
            
            list

            Comment


            • #7
              I did but for some reason i cannot get it right. And your solution only gets the first character (h) correct. The rest (ello) are positioned every other character: achuesltloomstring

              Comment


              • #8

                Indeed. My coding error. You described the problem as adding characters after positions 2, 4, 6, and so forth.

                But after adding a character after 2, what was at 4 is now at 5.

                And after adding a character after original 4 (new 5), what was at 6 is now at 8.

                And so forth.

                So the increment itself needs incrementing.

                Code:
                clear
                set obs 1
                gen whatever = "acustomstring"
                
                tokenize "h e l l o"
                local J : word count h e l l o
                
                mac li
                
                quietly forval j = 1/`J' {
                    local k = (2 * `j') + (`j' - 1)
                    replace whatever = substr(whatever, 1, `k') + "``j''" + substr(whatever, `k' + 1 , .)
                }
                
                list

                Comment


                • #9
                  Thanks Nick! I actually came pretty close but i did not think to add +1 in the second substr() function.

                  Comment

                  Working...
                  X