按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
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() processed English…Canadian formatted text and knew
that the identifier May equaled the fifth month of the year。 For the second Date。Parse() method
call; the culture was changed to German; and it was possible to process 10 Mai; 2008。 In both
cases; processing the buffer posed no major problems; as long as you knew that the buffer was
a German or English…Canadian date。 Where things can go awry is when you have a German
date and an English culture。
Converting a data type to a buffer is relatively easy because the ToString() methods have
been implemented to generate the desired output。 Consider the following example; which
generates a buffer from an integer value:
Public Sub TestGenerateString()
Dim iValue As Integer = 123
Dim buffer As String = iValue。ToString()
If buffer = 〃123〃 Then
Console。WriteLine( 〃correct〃)
End If
End Sub
In the example; the value 123 has been assigned to iValue; which is of type Integer; and
then its ToString() method is called; which generates a buffer that contains 〃123〃。 The same
thing can be done to a Double value; as in this example:
Dim number As Double = 123。5678
Dim buffer As String = number。ToString(〃0。00〃)
Here; the number 123。5678 is converted to a buffer using the ToString() method; but
ToString() has a parameter; which is a formatting instruction that indicates how the double
number should be generated as a buffer。 The desired result is a buffer with a maximum of two
digits after the decimal point。 Because the third digit after the decimal is a 7; the value is
rounded up; resulting in the buffer 123。57。
Let’s see an example where the culture information also applies to generating a buffer。
Here; a Double value is generated in the format of the culture:
…………………………………………………………Page 96……………………………………………………………
74 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