Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
18
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 25 OTHER TOPICS

Parsing Strings to Data Values

Strings are arrays of Unicode characters. For example, string "25.873" is six characters long and is not a number. Although it looks like a number, you cannot perform arithmetic functions on it. “Adding” two strings produces their concatenation.

Parsing allows you to take a string that represents a value and convert it into an actual value.

All the predefined, simple types have a static method called Parse, which takes a string value representing the type and converts it into an actual value of the type.

If the string cannot be parsed, the system raises an exception.

The following statement shows an example of the syntax of using a Parse method. Notice that Parse is static, so you need to invoke it by using the name of the target type.

double d1 = double.Parse("25.873");

Target type

String to be converted

The following code shows an example of parsing two strings to values of type double and then adding them:

static void Main()

{

string s1 = "25.873"; string s2 = "36.240";

double d1 = double.Parse(s1); double d2 = double.Parse(s2);

double total = d1 + d2; Console.WriteLine("Total: {0}", total);

}

This code produces the following output:

Total: 62.113

Note A common misconception about Parse is that since it operates on a string, it is thought of as a member of the string class. It is not. Parse is not a single method at all but a number of methods implemented by the target types.

672

CHAPTER 25 OTHER TOPICS

The disadvantage of the Parse methods is that they throw an exception if they can’t successfully parse the string to the target type. Exceptions are expensive operations, and you should try to programmatically avoid them if you can. The TryParse method allows you to do that. The important things to know about TryParse are the following:

Every built-in type that has a Parse method also has a TryParse method (and you should use the

TryParse).

The TryParse method takes two parameters and returns a bool.

The first parameter is the string you’re trying to parse.

The second is an out parameter of a reference to a variable of the target type.

If the TryParse succeeds, it returns true. Otherwise, it returns false.

The following code shows an example of using a TryParse method:

class Program

{

static void Main( )

{

bool success;

 

 

string

parseResultSummary;

 

string

stringFirst = "28";

 

int intFirst;

Input string

Output variable

 

 

success = int.TryParse( stringFirst, out intFirst );

parseResultSummary = success

?"was successfully parsed"

:"was not successfully parsed";

Console.WriteLine( "String {0} {1}", stringFirst, parseResultSummary );

string stringSecond = "vt750";

 

int intSecond;

Input string

Output variable

 

success = int.TryParse( stringSecond, out intSecond );

parseResultSummary = success

? "was successfully parsed"

: "was not successfully parsed"; Console.WriteLine( "String {0} {1}", stringSecond, parseResultSummary );

}

}

This code produces the following output:

String 28 was successfully parsed

String vt750 was not successfully parsed

673

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]