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

Posted on 2005-04-18
Medium Priority
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
  • 3
  • 2
LVL 10

Expert Comment

ID: 13806796
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 54

Accepted Solution

Ryan Chong earned 750 total points
ID: 13807008
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 54

Expert Comment

by:Ryan Chong
ID: 13807021
>> 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

ID: 13807061
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

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

809 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