Announcement

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

  • How to find out if a string contains a specific string in it?

    Hey,

    what should I use in a conditional statement in order to execute a command only on observations whose string variable (their name) contains a specific phrase?
    For example, I'm looking for something like:
    Code:
    replace var = x if name _____________
    where the "______________" part should check if a specific sequence of letters I'll define is part of name.

    Thanks in advance,
    Ben

  • #2
    Code:
    replace var = x if strpos(name, "_____________")>0
    or looking at your previous posts, if you are using non-English text,
    Code:
    replace var = x if ustrpos(name, "_____________")>0
    would probably be a better choice, and would work in either case, while strpos will only work with single-byte characters.
    Last edited by William Lisowski; 07 Jul 2019, 11:35.

    Comment


    • #3
      Thanks a lot William!

      Comment


      • #4
        Originally posted by William Lisowski View Post
        Code:
        replace var = x if strpos(name, "_____________")>0
        or looking at your previous posts, if you are using non-English text,
        Code:
        replace var = x if ustrpos(name, "_____________")>0
        would probably be a better choice, and would work in either case, while strpos will only work with single-byte characters.
        hi William, what if I am checking whether multiple strings is contained in a very long string?

        Comment


        • #5
          You would just use the or/and operators, depending on whether you want any of those multiple strings to figure, or all of them to figure in the string variable:
          Code:
          replace var = x if ustrpos(name, "xxxxxxxxxxxxxx")>0 | ustrpos(name, "yyyyyyyyyyyyyy")>0 | ustrpos(name, "zzzzzzzzzzz")>0
          
          OR
          
          replace var = x if ustrpos(name, "xxxxxxxxxxxxxx")>0 & ustrpos(name, "yyyyyyyyyyyyyy")>0 & ustrpos(name, "zzzzzzzzzzz")>0

          Comment

          Working...
          X