Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


August 16, 2004

Handle Strings More Easily

RSS
View this exclusive article with VIP access -- click here to join |
See More VBScript Articles Here | Reprints | Or sign up for our VIP Monthly Pass!

Download the Code Here

[Editor's Note: Share your scripting discoveries, comments, problems, solutions, and experiences with products. Email your contributions (500 words or less) to r2rwinscriptsol@winnetmag.com. We edit submissions for style, grammar, and length. If we print your submission, you'll get $100.]

One of the most annoying scripting problems is dealing with extraneous nonprinting characters in strings. Nonprinting characters (e.g., space, tab, carriage return, line feed) don't have visible symbols. The most common nonprinting characters have ASCII character codes in the 0 to 33 range. For example, text files often use the substitute character, which has an ASCII code of 26, and programming languages frequently use the null character, which has an ASCII code of 0. (For more information about characters' code representations, see the VBScript character table at http:// msdn.microsoft.com/library/en-us/ script56/html/vsmscANSITable.asp.)

You might not notice this problem when displaying data because an extra space or tab at the end of a line isn't apparent. However, if you use Microsoft Word to edit your scripts, particularly if you use Word's click-and-type formatting, Word might insert tabs and spaces at arbitrary locations. For example, the string "MyServer" might become "MyServer ".

As this example shows, nonprinting characters frequently occur at the beginning or end of a string. Although VBScript has functions that let you remove leading spaces (LTrim), trailing spaces (RTrim), and both leading and trailing spaces (Trim) from strings, these functions don't work with other nonprinting characters.

I wrote the SuperTrim function as an improvement to Trim. The script in Listing 1, illustrates how SuperTrim works. SuperTrim removes various nonprinting characters from the beginning and end of a string. You can use SuperTrim to make your scripts that manipulate strings deal more effectively with unreadable data. SuperTrim also lets you indent strings in files that you'll read for data without worrying about the effect of spaces or tabs because the code removes the troublesome special characters. Insert SuperTrim into your code and use it rather than Trim to remove nonprinting characters.

End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
CES 2009: Ballmer Announces Windows 7, Windows Live, Live Search Milestones

During his first-ever Consumer Electronics Show (CES) 2009 keynote address last night in Las Vegas, Microsoft CEO Steve Ballmer announced the pending public availability of a feature-complete Windows 7, the final version of Windows Live Essentials, and ...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

Where is Microsoft NetMeeting in Windows XP?

...


Related Articles Function Finds the nth Occurrence of Substrings

Related Events Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing