VBScript - How do I preform an operation if a part of a string is included in a longer string

Posted on 2005-04-18
Last Modified: 2010-04-06
I'm writing a script in VBScript and I'm writing an If/Then statment which needs to do the following:

Lets say Variable1 = "OU=sales,OU=mydomain,OU=COM"

How do I write an If/Then statment to effectively say: "IF Variable1 contains the word "sales" THEN...

FYI, I want my script to run on all users in an OU except those in the Sales OU.
Question by:gran3085
    LVL 10

    Expert Comment

    Dim v1, v2
    v2="Check to see if inside this string"

    If InStr(v2,v1)=0 Then
      MsgBox "Does not exist"
      Msgbox "Exists"
    End If
    LVL 48

    Accepted Solution

    it's always better to have a split before compare it with the instr function, try like:

    Function isInList(mList, SearchStr)
      tmpArr = Split(mList , ",")

      isInList = false
      for i = 0 to ubound(tmpArr)

       tmpArr2 = Split(tmpArr(i) , "=")

       if ubound(tmpArr2) = 1 then
          if trim(tmpArr2(1)) = SearchStr then
              isInList = true
              exit for      
          end if  
       end if  
    end function

    'test like:

    Variable1 = "OU=sales,OU=mydomain,OU=mydomain,OU=COM"
    SearchStr = "sales"

    msgbox SearchStr & ": " & isInList(Variable1, SearchStr)

    Variable1 = "OU=sales,OU=mydomain,OU=mydomain,OU=COM"
    SearchStr = "my"

    msgbox SearchStr & ": " & isInList(Variable1, SearchStr)

    LVL 48

    Expert Comment

    by:Ryan Chong
    >> it's always better to have a split before compare it with the instr function..
    sorry, i use a split, follow by a direct string compare, no use instr function on my example above. cheers
    LVL 10

    Expert Comment

    understandable if there is a chance the string could contain something like:


    something like this will match "he" with instr since "the" contains he and "hello" contains he

    whereas ryancys's method will return false
    LVL 10

    Expert Comment

    wow ... i guess i didn't actually look at your example very well ... i see thats what you did in your example =)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Android App Development

    This course will involve creating widgets, customize list view, grid view, spinners, etc. Creating applications using audio, video, and SQLite database. Ending with publication on Google Play.

    Suggested Solutions

    Preface In the first article: A Better Website Login System ( I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
    Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
    Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
    The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now