Announcement

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

  • Remove part of string before a character

    If I have string var with many entries that have the following: "numtimes15:blue", "numtimes2343:red", etc., and I wish to always remove everything before the colon (including the colon), what is the most efficient way of doing that? I tried first with subinstr() but because there are different characters that come before the colon it was not successful.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str16 var1
    "numtimes15:blue"
    "numtimes2343:red"
    end
    
    gen colon_pos = strpos(var1, ":")
    gen wanted = substr(var1, colon_pos+1, .)

    Comment


    • #3
      Or with regular expressions:
      Code:
      gen wanted = ustrregexra(var1,"^(.+):","")

      Comment


      • #4
        If "efficient" is running time, a faster regex solution is
        Code:
        gen wanted3 = regexs(1) if regexm(var1,":(.*)")
        #2 is fastest (2 mill obs / 10 reps):
        Code:
        . timer list
           2:      3.31 /       10 =       0.3313    strpos() + substr()
           3:     34.89 /       10 =       3.4894    ustrregexra(var1,"^(.+):","")
           4:      7.72 /       10 =       0.7716    regexm(var1,":(.*)")

        Comment

        Working...
        X