Announcement

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

  • Truncate string with different numbers of characters

    Hi,
    I am trying to drop the last four characters of a string variable with different numbers of letters. All the characters to be removed are exactly ".wav" and they are always the last four characters.
    Substring won't work because I cannot count position from the left. Any other ideas?
    Thank you,
    Emma

  • #2
    -subinstr- instead:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str9 var1
    "a.wav"    
    "aa.wav"  
    "aaaaa.wav"
    end
    
    clonevar new_var1=var1
    replace new_var1=subinstr(var1, ".wav", "", .)
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      you say you "cannot count position from the left." but you want to count from the right since you want the last 4 characters; see
      Code:
      help f_substr
      you will see that at least two of the examples do exactly that

      Comment


      • #4
        Thank you, the text substitution command worked.

        Comment

        Working...
        X