梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
simple way to concatenate string buffers together。
However�察�concatenation is not the aim of the example。 The aim is to treat the strings as
numbers and then add the numbers so that c contains the value 3 ��1 �� 2 = 3��。 The rewritten
version of the example is shown in Figure 3´14。 This code parses a string into an integer。
Integer is a value type�察�but
recognizes the methods associated
with the value types that can be used
to parse string buffers
Dim a As Integer = Integer。Parse�─�1;��
Dim b As Integer = Integer。Parse�─�2;��
Dim c As Integer = a �� b
Variable c contains Variables a and b are assigned
the value 3 values that represent the
parsed string buffers
Figure 3´14。 Parsing strings into integers before doing the arithmetic
The type Integer has a Parse�┌� method that can be used to turn a string into an integer。
The parsing works only if the buffer is a valid number。 If the buffer contains letters or an invalid
number�察�an error will be generated。
If the code can cope with a failed string conversion�察�the solution used by the parsing
routines is to generate an exception that a program could process。 Alternatively�察�a fail´safe way
to parse a number is to use TryParse�┌��察�as in the following example。
Dim value As Integer
If Integer。TryParse�─�1;�察�value�� Then
。 。 。
End If
The TryParse�┌� method does not return an integer value�察�but instead returns a Boolean
flag�察�indicating whether the buffer could be parsed。 If the return value is True�察�then the buffer
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 �C10 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