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  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
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


    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

    - More Windows Scripting Workarounds from Nilpo
    - Overloading Methods and More in VBScript
    - Improving MFC for Windows Vista
    - Regular Expressions in VBScript
    - Working with Dates in WMI
    - Completing Calendars with VBScript Date Func...
    - Building Calendars with VBScript Date Functi...
    - Working With Dates and Times in VBScript
    - Designing WCF DataContract Classes Using the...
    - Understanding Dates and Times in VBScript
    - Working With Arrays in VBScript
    - Compressed Folders in WSH
    - Using .NET Interops in VBScript
    - Nilpo`s Scripting Secrets, Vol I
    - Database operations using Silverlight 2.0 WC...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT