Windows Scripting
  Home arrow Windows Scripting arrow Page 3 - VBScript: Strings, You Can`t Function with...
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: Strings, You Can`t Function without Them
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2008-04-28

    Table of Contents:
  • VBScript: Strings, You Can`t Function without Them
  • LTrim That Hair You Hippie!
  • Trim()ming the Fat
  • Comparing Strings
  • The String Function
  • StrReverse()

  • 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: Strings, You Can`t Function without Them - Trim()ming the Fat


    (Page 3 of 6 )

    I know what you are thinking, so hold off on those e-mails. If you want to get rid of spaces on both sides of a string, you can do so with LRTrim(). No wait, no you can't. What were you thinking? You can however, use Trim() by itself. Here is how:


    <html>

    <body>

    <script type="text/vbscript">

    dim s

    s=" We are the Space Invaders! We have come to...well...invade your space! "

    document.write(Trim(s)) & document.write("Look out we've collided with some text because you ate our spaces!")

    </script>

    </body>

    </html>

    The result:

      Look out we've collided with some text because you ate our spaces!We are the Space Invaders! We have come to...well...invade your space!

    Why LTrim, RTrim, and Trim() Don't Actually Seem to Work

    You may have noticed when you insert your Trim() functions into HTML they don't quite work as you might think. Or better yet, you may have noticed that you don't really need them. This is because in HTML, extra spaces are automatically deleted. And if you use the technique to make sure your spaces don't get deleted, then, well, it wouldn't make sense to delete those spaces. Is that clear? Wait, where am I?

    The Replace() Function

    This handy little function is used when you want to replace a portion of a string with another string, a given number of times. Consider the following code:


    <html>

    <body>

    <script type="text/vbscript">

    dim example

    example="In a land far, far, far, far, I mean really far away..."

    document.write(Replace(example,"far","near"))

    </script>

    </body>

    </html>

    In this example, we replace all instances of the word "far" with the word "near." Sometimes however we might not want to replace every instance. In these situations, we can use some of the optional components of the Replace() function:


    <html>

    <body>

    <script type="text/vbscript">

    dim example

    example="In a land far, far, far, far, I mean really far away..."

    document.write(Replace(example,"far","near",1,1))

    </script>

    </body>

    </html>

    In this example you will notice that we made a change; we added to the expression: document.write(Replace(example,"far","near")) and changed it to document.write(Replace(example,"far","near",1,1)). The first 1 is an optional value you can add, which tells the program to begin in position one of the string (with the first character of the string). The second one is also optional, and tells the program how many times to replace our word. The result of this code is:

      In a land near, far, far, far, I mean really far away...

    If we change the position of where the replace starts, we get an odd result:


    <html>

    <body>

    <script type="text/vbscript">

    dim example

    example="In a land far, far, far, far, I mean really far away..."

    document.write(Replace(example,"far","near",10,1))

    </script>

    </body>

    </html>

    The result:

      near, far, far, far, I mean really far away...

    And if we want to replace two instances of a given term, here is how we do it:


    <html>

    <body>

    <script type="text/vbscript">

    dim example

    example="In a land far, far, far, far, I mean really far away..."

    document.write(Replace(example,"far","near",1,2))

    </script>

    </body>

    </html>

    Here is the print out:

      In a land near, near, far, far, I mean really far away...

    More Windows Scripting Articles
    More By James Payne


       · Thanks for stopping by to read my article on String functions in VBScript. Here we...
     

    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