So, you know everything about text, right? – part XV

In the previous posts, we took a deep dive into how we can format objects into strings. In this post, we’ll see how to obtain an object from a string (a process known as parsing). By convention, all the types that can parse a string offer a static method named Parse which (at a minimum) expects a string as a parameter. Currently, many of the times introduced by the framework are able to do parsing. For instance, the next snippet shows how to parse an integer from a string:

var intInStr = "100";
var aux = Int32.Parse( intInStr );

Besides the “simple” Parse method, Int32 offers other overloads of this method:

public static int Parse(string s, NumberStyles style)
public static int Parse(string s, IFormatProvider provider)
public static int Parse(string s, NumberStyles style, IFormatProvider provider)

If you’ve been following the series on text and strings, then you should probably understand what these parameters do. The first parameter (string s) identifies the string which is going to be parsed. NumberStyles is a flags enum which determines the styles permitted in the numeric strings (for instance, AllowExponent indicates that the string can contain a numeric value in exponential form). Finally, IFormatProvider references an object which the parse method can use to obtain culture specific information.The Parse method throws an exception when the passed value doesn’t match the expected numeric string:

//to make it work, you need to pass
//at least the NumberStyles.AllowExponent
var aux = Int32.Parse( "10e2");

If you run the previous code, you’ll end up with an exception because (by default) the simple Parse call uses NumberStyles.Integer for the style parameter. The solution to the problem is simple: pass the NumberStyles.AllowExponent value to the style parameter. Don’t forget that the IFormatProvider plays also an important role in the parsing. For instance, take a look at the following code:

var aux = Double.Parse( "10,2", NumberStyles.Any, new CultureInfo("pt-PT"));

Specifying the pt-PT culture is necessary for getting the double value 10.2. If you had passed the en-US culture, then you’d end up with the value 102 because the char ‘,’ isn’t used as decimal separator in that culture (I believe that in en-US the char ‘,’ is used as a thousands separator).

Since we’re talking about parsing, it’s important to mention that the DateTime type introduces a ParseExact method (besides the traditional Parse method). This method was added to the API of the type because several developers complained about the forgiveness of the original Parse method (after all, parsing dates isn’t really a walk in the park).

But the parsing of DateTime string values wasn’t the only thing developers complained about. Many people have also raised their voice against the implementation followed by the Parse methods. The problem is that there are some apps which need to receive lots of input from the users and if parsing ends up throwing lots of exceptions, then you’ve just degraded the performance of your app. To avoid a breaking change, MS added a new method for the parsing API recommendation: the TryParse method. Here are the overloads introduced by Int32:

public static bool TryParse(string s, out int result)
public static bool TryParse(string s, NumberStyles style, IFormatProvider provider, out int result)

The method returns true when the conversion is performed
successfully. In that case, result ends up with the parsed integer.

And I guess that’s it for now. Stay tuned for more.


~ by Luis Abreu on May 17, 2011.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: