Windows Scripting
  Home arrow Windows Scripting arrow Page 2 - VBScript: Functioning with Strings
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Actuate Whitepapers 
VeriSign Whitepapers 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
WINDOWS SCRIPTING

VBScript: Functioning with Strings
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2008-04-14

    Table of Contents:
  • VBScript: Functioning with Strings
  • Checking If It’s In With the InStr() Function
  • Using InStr() with Variables
  • Uppercase and Lowercase

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    VBScript: Functioning with Strings - Checking If It’s In With the InStr() Function


    (Page 2 of 4 )

    The InStr() function can be used for a number of purposes. For starters, you can use it to see if a particular string contains another character or string, as in the following sample:


    <html>

    <body>

    <script type="text/vbscript">

    dim toon

    dim where

    toon="George, George, George of the jungle."

    where=InStr(1,toon,"j",0)

    document.write(where) & "<br />"

    where=InStr(toon,"of")

    document.write(where)

    </script>

    </body>

    </html>

    In the above sample we assign a value to the variable toon, then use the InStr() function to check whether the character “j” is in the string. It then prints the position at which the first occurrence of “j” appears. Next we use InStr() to check whether the string “of” is in the toon variable. Once more, we print the position of the first occurrence of, well…”of”. Here is the result:

      31

      24

    This means if you count 31 characters (including spaces) into the variable, you will find “f”. Likewise, if you count 24 characters in, you will find the first occurrence of the word “of”.

    But what if a character or string is not found? Observe this code:


    <html>

    <body>

    <script type="text/vbscript">

    dim toon

    dim where

    toon="George, George, George of the jungle."

    where=InStr(1,toon,"o",0)

    document.write(where) & "<br />"

    where=InStr(toon,"forest")

    document.write(where)

    </script>

    </body>

    </html>

    Here we search for the character “o” and the string “forest”. The character “o” is found a number of times, but remember that InStr() only returns the position of the first occurrence. The string “forest” is not found, and so a “0” is returned.

    Here is the result:

      3

      0

    In all of the above examples, we begin our search with the first character in the toon variable. There is an option with the InStr() function that lets you decide from which position to begin searching. For instance, if we wanted to begin our search after the first word “George,” we would start the search position at 6, like so:


    <html>

    <body>

    <script type="text/vbscript">

    dim toon

    dim where

    toon="George, George, George of the jungle."

    where=InStr(6,toon,"o",0)

    document.write(where) & "<br />"

    where=InStr(toon,"forest")

    document.write(where)

    </script>

    </body>

    </html>

    As you can see, the program now returns the first occurrence of “o” at position 11, instead of 3, as in our previous example.

    Here is the result:

      11

      0

    You may also have noted in the above code (where=InStr(6,toon,"o",0) that after the criteria “o”, there was a 0. This is an optional piece of code that lets you specify whether to do a binary (0) or textual (1) search.

    More Windows Scripting Articles
    More By James Payne


       · You should have included the LIKE function. This function gives developers a...
       · Hey, thanks for the suggestion. I'll actually be doing an article on regex soon, and...
     

    WINDOWS SCRIPTING ARTICLES

    - Generating Outlook Signatures Based on Activ...
    - VBScript: Converting and Formatting with Fun...
    - VBScript: Conversion and Format Functions
    - VBScript: Array Functions
    - VBScript: Strings, You Can`t Function withou...
    - VBScript: More String Functions
    - VBScript: Functioning with Strings
    - Working with the Windows Registry in C++
    - Understanding Objects
    - HTML Applications: Giving WSH a User Interfa...
    - Modifying Computer Objects with Active Direc...
    - Logon Script to Send Email Notifications
    - Securing Computers and Active Directory
    - Moving and Renaming Computers with Active Di...
    - Working with System Processes in WSH





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway