Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Click here to receive your FREE subscription to Visual Studio Magazine

email article
printer friendly

String.Together Snazzy String Routines
String handling is one of the most basic—and important—capabilities that all developers must deal with. Learn how to take maximum advantage of the way VB .NET handles strings.
by Billy Hollis

April 23, 2007

Technology Toolbox: VB .NET, VB6, VB5

At some point, every developer needs to manipulate strings, whether it's to convert data from one format to another or to alphabetize a list. Strings might be a basic part of programming, but using them you encounter a lot of permutations, cases where using them can be more difficult or more complex than it might initially appear.

ADVERTISEMENT

This is particularly true for those developers who have transitioned or are thinking of transitioning to Visual Basic .NET from Classic Visual Basic, whether VB5, VB6, or some other version. Most of the Classic VB string functionality is still supported in VB .NET through the compatibility library, but using this library can set back your development as a .NET programmer. Relying on these old libraries can keep you from discovering the effective and highly useful new features that Microsoft provided with its new string handling classes.

Indeed, I find that I rarely use old VB string functions in my .NET development. The newer replacement string methods are more flexible and usually result in cleaner code. And I can write string-handling logic with fewer lines of code than before. If you're still using older VB string functions, or if you're writing a lot of code for loops and manual string construction, I can't emphasize enough how much more productive you'll be in .NET if you adopt the native string-handling techniques instead. Doing so will speed up your development and help you write tighter, more maintainable code.

I'll help get you started by walking you through some of the new capabilities in VB .NET's strings, pointing out similarities and differences, as well as places where you might run into trouble if you bring a Classic VB mentality with you to .NET. Note that this is a companion article to one that ran in the March 2007 issue of VSM ["Straighten Out Your Strings," VSM March 2007]. In that article, I detailed several techniques for dealing with strings, placing a special emphasis on comparing .NET's string handling capabilities to the classic VB equivalents. That article didn't come close to exhausting the interesting things you can do with strings in .NET, so I'm back this month with some more useful tips on string handling. No doubt, this is a rich enough subject that it would be possible to do another two or three articles after this one.

One of the best aspects of .NET's implementation of strings is that it can save you a lot of effort. I perform a lot of code reviews, and I often see developers formatting their strings the hard way, with a lot of manual code for the construction of a particular formatted string. I'm too lazy for that, and you should be, too.

Be Smart; Be Lazy
There's an entire subsystem in .NET that enables you to implement basic and advanced formatting with a minimal amount of code; much of that subsystem is accessible through the String class's Format method.

Covering all of the capabilities of the .NET string formatting would be an article in itself, but I'll cover some of the more important aspects you're likely to brush up against here. String.Format includes several overloads. Here's the simplest:

String.Format({FormattingString}, 
	{ObjectToFormat})

This method returns a formatted string based on two arguments. The first argument specifies how you want the string to be formatted, while the second argument holds the object or value you want to format. The second argument can be any of several different data types. Numbers, dates, and colors are common types to reformat, and .NET includes special formatting strings for all of them.




Back to top













Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTPOnline Home