Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


August 16, 2004

Handle Strings More Easily

RSS
Subscribe to Windows IT Pro | See More VBScript Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

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 be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
Command Prompt Tricks

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

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


Related Articles Function Finds the nth Occurrence of Substrings

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

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 Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


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 DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement