按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
could be parsed; and the result is stored in the parameter value。 You can parse other number
types using the same techniques (for example; Single。TryParse())。
There are more variations in how a number can be parsed。 For example; how would the
number 100 be parsed; if the number 100 is hexadecimal? (puters use hexadecimal numbers。)
Here is an example of hexadecimal conversion:
Imports System。Globalization
。 。 。
Public Sub ParseHexadecimal()
Dim value As Integer= Integer。Parse(〃100〃; NumberStyles。HexNumber)
End Sub
…………………………………………………………Page 93……………………………………………………………
CH AP T E R 3 ■ L E AR N IN G AB O U T ST R I N G M A N I PU L A TI O N S 71
This example uses a variant of Parse(); which has an additional second parameter that
represents the format of the number。 In this case; the second parameter indicates that the
format of the number is hexadecimal (NumberStyles。HexNumber; from the System。Globalization
namespace)。
■Note If you are wondering how 100 maps to 256 at the hex level; use the calculator that es with the
Windows operating system。 Switch the calculator to scientific view (View Scientific)。 Click the Hex radio
button; enter the number 100; and then click the Dec radio button。
The enumeration NumberStyles has other values that can be used to parse numbers according
to other rules。 For example; some rules handle the use of parentheses surrounding a number
to indicate a negative value。 Other rules deal with whitespace。 Here is an example:
Public Sub TestParseNegativeValue()
Dim value As Integer = Integer。Parse( 〃 (10) 〃; _
NumberStyles。AllowParentheses Or _
NumberStyles。AllowLeadingWhite Or _
NumberStyles。AllowTrailingWhite)
End Sub
The number 〃 (10) 〃 in this example is plicated in that it has whitespace and paren
theses。 Attempting to parse the number using Parse() without using any of the NumberStyles
enumerated values will not work。 The enumeration AllowParentheses processes the parentheses;
AllowLeadingWhite ignores the leading spaces; and AllowTrailingWhite ignores the trailing
spaces。 Then; when the buffer has been processed; a value of –10 will be stored in the variable
value。
Other NumberStyles enumerated values allow you to process decimal points for fractional
numbers; positive or negative numbers; and so on。 This then raises the topic of processing
numbers other than Integer。 Each of the base data types; such as Boolean; Byte; and Double;
has associated Parse() and TryParse() methods。 Additionally; the method TryParse() can use
the NumberStyles enumeration。 (See the MSDN documentation for details on the NumberStyles
enumerated values。)
Parsing integer values is the same; regardless of the country。 Parsing double values or dates
is not the same。 Consider the following example; which tries to parse a buffer that contains
decimal values。
Public Sub TestDoubleValue()
Dim value As Double = Double。Parse(〃1234。56〃)
value = Double。Parse(〃1;234。56〃)
End Sub
In this example; both uses of the Parse() method process the number 1234。56。 The first
Parse() method is a simple parse; because it contains only a decimal point separating the
whole number from the partial number。 The second Parse() method is more plicated in
…………………………………………………………Page 94……………………………………………………………
72 CH AP T E R 3 ■ L E A R N IN G AB OU T ST R I N G M A N I P U L AT IO N S
that a ma is used to separate the thousands of the whole number。 In both cases; the
Parse() routines did not fail。
If you test this code; it’s possible that an exception will be generated。 In this case; the culture of
the application is to blame。 The numbers presented in the example are encoded using en…CA;
which is English…Canadian notation。
Working with Cultures
In ; culture information is made up using two identifiers: language and specialization。 As
I mentioned earlier; in Switzerland; there are four spoken languages; which means that there
are four different ways of expressing a date; time; and currency。 This does not mean that the
date is different for German speakers and French speakers。 The date format will be identical;
but the words (Maerz or Mars for the month March) will be different。 The words for the date are
the same in Austria; Switzerland; and Germany; but the format is not identical。 This means
multilanguage countries such as Canada (French and English) and Luxembourg (French and
German) need to be able to process multiple encodings; hence the need for the two identifiers。
To retrieve the current culture; use the following code:
Dim info As System。Globalization。CultureInfo = _
System。Threading。Thread。CurrentThread。CurrentCulture
Console。WriteLine(〃Culture (〃 & info。EnglishName & 〃)〃)
The property Thread。CurrentThread。CurrentCulture retrieves the culture information
associated with the currently executing thread。 As a side note; it is possible to associate
different threads with different cultural information。 The property EnglishName generates an
English version of the culture information; which would appear similar to the following:
Culture (English (Canada))
Consider the following number:
1;234
The number with an American or Canadian culture is one thousand two hundred thirty
four; but with a German culture; it is one point two three four (for those who do not know about
German formatting; a ma is used as a decimal separator; and a period is used as a thousands
separator)。 One way to change the culture is with the dialog box shown earlier in Figure 3…12。 The
second way to change the culture is at a programmatic level; as in this code:
Imports System。Threading
Thread。CurrentThread。CurrentCulture = new CultureInfo(〃en…CA〃)
In the example; a new instance of CultureInfo is created with the culture information en…CA。
Next is an example that processes a double number encoded using German formatting rules:
Public Sub TestGermanParseNumber()
Thread。CurrentThread。CurrentCulture = new CultureInfo(〃de…DE〃)
Dim value As Double = Double。Parse(〃1;234〃)
End Sub
This example assigns the de…DE culture information to the currently executing thread。 Then
whenever any parsing routines are used; German from Germany is used as the basis for the
…………………………………………………………Page 95……………………………………………………………
CH AP T E R 3 ■ L E AR N IN G AB O U T ST R I N G M A N I PU L A TI O N S 73
formatting rules。 Changing the culture information does not affect the formatting rules of the
programming language。
It is also possible to parse dates and times using the Parse() and TryParse() routines; as
demonstrated by the following examples:
Public Sub TestGermanParseDate()
Dim datetime As Date = Date。Parse(〃May 10; 2008〃)
If 5 = datetime。Month Then
Console。WriteLine( 〃correct〃)
End If
Thread。CurrentThread。CurrentCulture = new CultureInfo(〃de…DE〃)
datetime = Date。Parse(〃10 Mai; 2008〃)
If 5 = datetime。Month Then
Console。WriteLine( 〃correct〃)
End If
End Sub
Notice how the first Date。Parse() p