Announcement

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

  • Delete portion of a string following a unique set of characters

    I have a string variable with many observations. Some of the observations have a set of characters, //////, that is then followed by some more text, but different text in each case. I would like to delete the /////// and what follows. I'm hoping to find a way to delete starting at /////// at anything after, but to keep what comes before the ///////.

    Here's is an example:
    oldstring:
    xxxxxx///////skflajf
    rrrr333///////dosifjw
    wwwwww

    newstring:
    xxxxxx
    rrrr333
    wwwwww



    Thanks!

  • #2
    Try gen newstr = substr(oldstr, 1, strpos(oldstr, "///////")-1) Where the number of slashes should be what you want (I'm using a small device to read this).

    Comment


    • #3
      Plus replace newstr = oldstr if missing(newstr)

      Comment


      • #4
        And here's how you can do this using regular expressions

        Code:
        gen newstr = regexr(oldstr,"///.+","")
        The pattern "///.+" will match a substring that starts with "///". The following "." is a wildcard that matches any single character,. The "+" will match one or more of the preceding character. The matched substring is then replaced with an empty string.

        Comment

        Working...
        X