.NET Tips and Tricks

Blog archive

Is There Anything in that String?

String variables look like value types, those variables holding integers and dates. But, under the hood, strings are more like reference types, those variables that point to objects. This means that you can set a string to Nothing/null, something you can't do with integers (though you can with a variation on some value types). While a string variable set to Nothing/null is obviously a string with 'nothing there,' you probably also consider a string set to String.Empty or "" (a zero-length string, or ZLS) as 'nothing there.' In some conditions, you may even consider a bunch of blanks ("     ") as 'nothing there.'

.NET provides a method for checking these conditions, and .NET 4 adds a new version that does a little more. The earlier method is the static/shared method IsNullOrEmpty, which returns True if a string is set to any of String.Empty, null/Nothing or a ZLS, but returns false for a string of blanks:

Dim res As Boolean
Dim tester As String
tester = Nothing
res = String.IsNullOrEmpty(tester) 'res is True
tester = String.Empty
res = String.IsNullOrEmpty(tester) 'res is True
tester = ""
res = String.IsNullOrEmpty(tester) 'res is True
tester = "    "
res = String.IsNullOrEmpty(tester) 'res is False

.NET 4 adds a new method named IsNullOrWhitespace which does the same thing as IsNullOrEmpty, but also returns True for a string of blanks or anything else that .NET classifies as whitespace. Whitespace is anything that returns True from char.IsWhiteSpace and includes, for example, tabs and carriage returns/linefeeds:

tester = vbTab
res = String.IsNullOrWhiteSpace(tester) 'res is True
The key issue in choosing between the two is whether you consider a string of blanks/tabs/carriage returns/linefeeds as 'nothing there' (use IsNullOrWhitespace) or as 'something there' (use IsNullOrEmpty).

Posted by Peter Vogel on 09/11/2013


comments powered by Disqus

Featured

  • AdaBoost Binary Classification Using C#

    Dr. James McCaffrey from Microsoft Research presents a C# program that illustrates using the AdaBoost algorithm to perform binary classification for spam detection. Compared to other classification algorithms, AdaBoost is powerful and works well with small datasets, but is sometimes susceptible to model overfitting.

  • From Core to Containers to Orchestration: Modernizing Your Azure Compute

    The cloud changed IT forever. And then containers changed the cloud. And then Kubernetes changed containers. And then microservices usurped monoliths, and so it goes in the cloudscape. Here's help to sort it all out.

  • The Well-Architected Architect on Azure

    In the dynamic field of cloud computing, the architect's role is increasingly pivotal as they must navigate a complex landscape, considering everything from the overarching architecture and individual service configurations to the various trade-offs involved. Here's help.

  • Windows Community Toolkit Update Improves Controls

    The Windows Community Toolkit advanced to version 8.1, adding new features, improving existing controls and making dependency changes.

Subscribe on YouTube