Parsing a String to Obtain an Object

Crypto Ultimatum

How to make Money by Investing in Cryptocurrency

Get Instant Access

In the preceding section, I explained how to take an object and obtain a string representation of that object. In this section, I'll talk about the opposite: how to take a string and obtain an object representation of it. Obtaining an object from a string isn't a very common operation, but it does occasionally come in handy. Microsoft felt it necessary to formalize a mechanism by which strings can be parsed into objects.

Any type that can parse a string offers a public, static method called Parse. This method takes a String and returns an instance of the type; in a way, Parse acts like a constructor. In the FCL, a Parse method exists on all the numeric types as well as for DateTime, TimeSpan, and a few other types (like the SQL data types).

Let's look at how to parse a string into a number type. All the numeric types (Byte, SByte, Int16/UInt16, Int32/UInt32, Int64/UInt64, Single, Double, and Decimal) offer at least one Parse method. Here I'll show you just the Parse method defined by the int32 type. (The Parse methods for the other numeric types are identical.)

public static Int32 Parse(String s, NumberStyles style,

IFormatProvider provider);

Just from looking at the prototype, you should be able to guess exactly how this method works. The string parameter, s, identifies a string representation of a number you want parsed into an int32 object. The system.Globalization.-

Numberstyles parameter, style, is a set of bit flags that identify characters that Parse should expect to find in the string. And the iFormatProvider parameter, provider, identifies an object that the Parse method can use to obtain culture-specific information as discussed earlier in this chapter.

For example, the following code causes Parse to throw a system.Format-Exception exception because the string being parsed contains a leading space:

Int32 x = Int32.Parse(" 123", NumberStyles.None, null);

To allow Parse to skip over the leading space, change the style parameter as follows:

Int32 x = Int32.Parse(" 123", NumberStyles.AllowLeadingWhite, nul l);

Table 12-5 shows the bit symbols that the Numberstyles type defines. Table 12-5: Bit Symbols Defined by the Numberstyles Type

Symbol

Value

Description

None

0x00000000

None of the special characters represented by any of the bits in the remaining rows in this table are allowed in the string.

AllowLeadingWhite

0x00000001

The string can contain leading/trailing white-space characters (identified by the following Unicode code points: 0x0009, 0x000A, 0x000B, 0x000C,0x000D,and 0x0020).

AllowTrailingWhite

0x00000002

AllowLeadingsign

0x00000004

The string can contain a valid leading/trailing sign character. NumberFormatinfo's Positivesign and Negativesign properties determine valid leading-sign characters.

AllowTrailingsign

0x00000008

AllowParentheses

0x00000010

The string can contain parentheses.

AllowDecimalPoint

0x00000020

The string can contain a valid decimal-separator character. NumberFormatinfo's NumberDecimalseparator and CurrencyDecimalseparator propertie s determine valid decimal-separator

Table 12-5: Bit Symbols Defined by the NumberStyles Type

Symbol

Value

Description

characters.

AllowThousands

0x00000040

The string can contain a valid grouping-separator character. NumberFormatInfo's NumberGroupSeparator and CurrencyGroupSeparator properties determine valid grouping-separator characters. NumberFormatInfo's NumberGroupSizes and CurrencyGroupSizes properties determine the number of digits in the group.

AllowExponent

0x00000080

The string can contain a number expressed in exponent format: {e|E} [{+|-}] n where n is a number.

AllowCurrencySymbol

0x00000100

The string can contain a valid currency symbol, which NumberFormatInfo's CurrencySymbol property determines.

AllowHexSpecifier

0x00000200

The string can contain hex digits (0D9, ADF), and the string is considered to be a hex value.

In addition to the bit symbols in Table 12-5, the NumberStyles enumerated type also defines some symbols that represent common combinations of the individual bits. Table 12-6 shows these.

Table 12-6: Symbols for NumberStyles's Bit Combinations

In addition to the bit symbols in Table 12-5, the NumberStyles enumerated type also defines some symbols that represent common combinations of the individual bits. Table 12-6 shows these.

Table 12-6: Symbols for NumberStyles's Bit Combinations

Symbol

Bit Set

Integer

AllowLeadingWhite AllowLeadingSign

AllowTrailingWhite |

Number

AllowLeadingWhite

AllowLeadingSign

AllowDecimalPoint

AllowTrailingWhite | AllowTrailingSign | AllowThousands

Float

AllowLeadingWhite

AllowLeadingSign

AllowExponent

AllowTrailingWhite | AllowDecimalPoint |

Currency

AllowLeadingWhite AllowLeadingSign AllowParentheses AllowThousands

AllowTrailingWhite | AllowTrailingSign | AllowDecimalPoint | AllowCurrencySymbol

HexNumber

AllowLeadingWhite

AllowTrailingWhite |

Table 12-6: Symbols for NumberStyles's Bit Combinations

Symbol

Bit Set

AllowHexSpecifier

Any

AllowLeadingWhite | AllowTrailingWhite | AllowLeadingSign | AllowTrailingSign | AllowParentheses | AllowDecimalPoint | AllowThousands | AllowCurrencySymbol | AllowExponent

Here's a code fragment showing how to parse a hexadecimal number:

Int32 x = Int32.Parse("1A", NumberStyles.HexNumber, null); Console.WriteLine(x); // Displays "26"

This Parse method accepts three parameters. For convenience, many types offer additional overloads of Parse so that you don't have to pass as many arguments. For example, Int32 offers four overloads of the Parse method:

// Passes NumberStyles.Integer for style

// and null for provider parameters.

public static Int32 Parse(String s);

// Passes null for the provider parameter.

public static Int32 Parse(String s, NumberStyles style);

// Passes NumberStyles.Integer for the style parameter. public static Int32 Parse(String s, IFormatProvider provider) {

// This is the method I've been talking about in this section. public static int Parse(String s, NumberStyles style, IFormatProvider provider);

The DateTime type also offers a Parse method:

public static DateTime Parse(String s,

IFormatProvider provider, DateTimeStyles styles);

This method works just like the Parse method defined on the number types except that DateTime's Parse method takes a set of bit flags defined by the DateTimeStyles enumerated type instead of the NumberStyles enumerated types. Table 12-7 shows the bit symbols that the DateTimeStyles type defines.

Table 12-7: Bit Symbols Defined by the DateTimeStyles Type

Symbol

Value

Description

None

0x00000000

None of the special characters represented

Table 12-7: Bit Symbols Defined by the DateTimeStyles Type

Symbol

Value

Description

by the bits in the remaining rows in this table are allowed in the string.

AllowLeadingWhite

0x00000001

The string can contain leading/trailing/inner white-space characters(identified by the following Unicode code points: 0x0009, 0x000A, 0x000B, 0x000C, 0x000D,and 0x0020).

AllowTrailingWhite

0x00000002

AllowInnerWhite

0x00000004

NoCurrentDateDefault

0x00000008

When parsing a string that contains only a time (no date), set the date to January 1, 0001 instead of the current date.

AdjustToUniversal

0x00000010

When parsing a string that contains a timezone specifier ("GMT", "Z", "+xxxx", " xxxx"), adjust the parsed time base to Greenwich Mean Time.

In addition to these bit symbols, the DateTimeStyles enumerated type also defines an AllowWhiteSpaces symbol that represents all the White symbols OR'd together (AllowLeadingWhite | AllowInnerWhite | AllowTrailingWhite).

In addition to these bit symbols, the DateTimeStyles enumerated type also defines an AllowWhiteSpaces symbol that represents all the White symbols OR'd together (AllowLeadingWhite | AllowInnerWhite | AllowTrailingWhite).

For convenience, the DateTime type offers three overloads of the Parse method:

// null for formatProvider and DateTimeStyles.None public static DateTime Parse(String s);

// DateTimeStyles.None public static DateTime Parse(String s, IFormatProvider provider);

// This is the method I've been talking about in this section. public static DateTime Parse(String s,

IFormatProvider provider, DateTimeStyles styles);

Parsing dates and times is complex. Many developers have found DateTime's Parse method too forgiving in that it sometimes parses strings that don't contain dates or times. For this reason, the DateTime type also offers a ParseExact method that accepts a picture format string that indicates exactly how the date/time string is formatted and how it should be parsed. For more information about picture format strings, see the DateTimeFormatInfo class in the .NET Framework SDK.

Was this article helpful?

0 0

Post a comment