Geeks With Blogs

News INETA Community Speakers Program
GeeksWithBlogs.net: WTFNext's hosting!

View Stacy Vicknair's profile on LinkedIn


WTF Next? Dev ramblings from a master of nothing.

When we use string so often for its value-like behavior, it is easy to forget that the String class comes with its own constructors, and that there are some tasks you can achieve with the string constructor that you might have overlooked otherwise.

Repeating characters

One of the more useful overrides of the String constructor is the ability to easily repeat any character a number of times. Here’s a brief example:

Dim horizontalRule As New String("-"c, 100)
Console.WriteLine(horizontalRule)

Subsections of Char arrays

If you’ve got a Char array you can grab a subsection by specifying a start point and a length:

Dim catchyPhrase As Char() = CType("Catch for us the foxes.", Char())
Dim phrasePart As New String(catchyPhrase, 0, 12)

Console.WriteLine(phrasePart)

Other pointer based operations

There are a few more useful constructors if you are developing in C# or C++ that make use of arrays, but because Visual Basic doesn’t allow for the use of unsafe types I won’t go over them. You can check out the remaining constructors on the MSDN at the link below.

There are many useful features in .NET that can be easily overlooked, but in the long run you might find something that would have otherwise saved you time.

http://msdn.microsoft.com/en-us/library/system.string.string.aspx

 

Technorati Tags: ,,
Posted on Monday, May 4, 2009 9:31 AM VB .NET , .NET Tips | Back to top


Comments on this post: .NET Tip: Using String Constructors

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Stacy Vicknair | Powered by: GeeksWithBlogs.net