VB.Net Tutorial/Data Type/Char Function

Материал из VB Эксперт
Версия от 12:54, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Char.IsDigit

public class Test
   public Shared Sub Main
        Dim Char1 As Char
        Char1 = "A"
        If Char.IsDigit(Char1) Then
            Console.WriteLine("The character is a digit.")
        Else
            Console.WriteLine("The character is a letter or symbol.")
        End If
   End Sub
   
End class
The character is a letter or symbol.

Char: IsLetter, IsControl, IsDigit, IsLetterOrDigit, IsLower, IsNumber

Public Class Tester
    Public Shared Sub Main
        Dim result As New System.Text.StringBuilder
        Dim counter As Integer
        Dim testChar As Char
        Dim testHex As String
        For counter = 0 To 127
            testChar = Chr(counter)
            testHex = "\x" & Hex(counter)
            If Char.IsLetter(testChar) Then _
               result.AppendLine(testHex & "   IsLetter")
            If Char.IsControl(testChar) Then _
               result.AppendLine(testHex & "   IsControl")
            If Char.IsDigit(testChar) Then _
               result.AppendLine(testHex & "   IsDigit")
            If Char.IsLetterOrDigit(testChar) Then _
               result.AppendLine(testHex & "   IsLetterOrDigit")
            If Char.IsLower(testChar) Then _
               result.AppendLine(testHex & "   IsLower")
            If Char.IsNumber(testChar) Then _
               result.AppendLine(testHex & "   IsNumber")
        Next counter
        Console.WriteLine(result)
    End Sub
End Class
\x0   IsControl
\x1   IsControl
\x2   IsControl
\x3   IsControl
\x4   IsControl
\x5   IsControl
\x6   IsControl
\x7   IsControl
\x8   IsControl
\x9   IsControl
\xA   IsControl
\xB   IsControl
\xC   IsControl
\xD   IsControl
\xE   IsControl
\xF   IsControl
\x10   IsControl
\x11   IsControl
\x12   IsControl
\x13   IsControl
\x14   IsControl
\x15   IsControl
\x16   IsControl
\x17   IsControl
\x18   IsControl
\x19   IsControl
\x1A   IsControl
\x1B   IsControl
\x1C   IsControl
\x1D   IsControl
\x1E   IsControl
\x1F   IsControl
\x30   IsDigit
\x30   IsLetterOrDigit
\x30   IsNumber
\x31   IsDigit
\x31   IsLetterOrDigit
\x31   IsNumber
\x32   IsDigit
\x32   IsLetterOrDigit
\x32   IsNumber
\x33   IsDigit
\x33   IsLetterOrDigit
\x33   IsNumber
\x34   IsDigit
\x34   IsLetterOrDigit
\x34   IsNumber
\x35   IsDigit
\x35   IsLetterOrDigit
\x35   IsNumber
\x36   IsDigit
\x36   IsLetterOrDigit
\x36   IsNumber
\x37   IsDigit
\x37   IsLetterOrDigit
\x37   IsNumber
\x38   IsDigit
\x38   IsLetterOrDigit
\x38   IsNumber
\x39   IsDigit
\x39   IsLetterOrDigit
\x39   IsNumber
\x41   IsLetter
\x41   IsLetterOrDigit
\x42   IsLetter
\x42   IsLetterOrDigit
\x43   IsLetter
\x43   IsLetterOrDigit
\x44   IsLetter
\x44   IsLetterOrDigit
\x45   IsLetter
\x45   IsLetterOrDigit
\x46   IsLetter
\x46   IsLetterOrDigit
\x47   IsLetter
\x47   IsLetterOrDigit
\x48   IsLetter
\x48   IsLetterOrDigit
\x49   IsLetter
\x49   IsLetterOrDigit
\x4A   IsLetter
\x4A   IsLetterOrDigit
\x4B   IsLetter
\x4B   IsLetterOrDigit
\x4C   IsLetter
\x4C   IsLetterOrDigit
\x4D   IsLetter
\x4D   IsLetterOrDigit
\x4E   IsLetter
\x4E   IsLetterOrDigit
\x4F   IsLetter
\x4F   IsLetterOrDigit
\x50   IsLetter
\x50   IsLetterOrDigit
\x51   IsLetter
\x51   IsLetterOrDigit
\x52   IsLetter
\x52   IsLetterOrDigit
\x53   IsLetter
\x53   IsLetterOrDigit
\x54   IsLetter
\x54   IsLetterOrDigit
\x55   IsLetter
\x55   IsLetterOrDigit
\x56   IsLetter
\x56   IsLetterOrDigit
\x57   IsLetter
\x57   IsLetterOrDigit
\x58   IsLetter
\x58   IsLetterOrDigit
\x59   IsLetter
\x59   IsLetterOrDigit
\x5A   IsLetter
\x5A   IsLetterOrDigit
\x61   IsLetter
\x61   IsLetterOrDigit
\x61   IsLower
\x62   IsLetter
\x62   IsLetterOrDigit
\x62   IsLower
\x63   IsLetter
\x63   IsLetterOrDigit
\x63   IsLower
\x64   IsLetter
\x64   IsLetterOrDigit
\x64   IsLower
\x65   IsLetter
\x65   IsLetterOrDigit
\x65   IsLower
\x66   IsLetter
\x66   IsLetterOrDigit
\x66   IsLower
\x67   IsLetter
\x67   IsLetterOrDigit
\x67   IsLower
\x68   IsLetter
\x68   IsLetterOrDigit
\x68   IsLower
\x69   IsLetter
\x69   IsLetterOrDigit
\x69   IsLower
\x6A   IsLetter
\x6A   IsLetterOrDigit
\x6A   IsLower
\x6B   IsLetter
\x6B   IsLetterOrDigit
\x6B   IsLower
\x6C   IsLetter
\x6C   IsLetterOrDigit
\x6C   IsLower
\x6D   IsLetter
\x6D   IsLetterOrDigit
\x6D   IsLower
\x6E   IsLetter
\x6E   IsLetterOrDigit
\x6E   IsLower
\x6F   IsLetter
\x6F   IsLetterOrDigit
\x6F   IsLower
\x70   IsLetter
\x70   IsLetterOrDigit
\x70   IsLower
\x71   IsLetter
\x71   IsLetterOrDigit
\x71   IsLower
\x72   IsLetter
\x72   IsLetterOrDigit
\x72   IsLower
\x73   IsLetter
\x73   IsLetterOrDigit
\x73   IsLower
\x74   IsLetter
\x74   IsLetterOrDigit
\x74   IsLower
\x75   IsLetter
\x75   IsLetterOrDigit
\x75   IsLower
\x76   IsLetter
\x76   IsLetterOrDigit
\x76   IsLower
\x77   IsLetter
\x77   IsLetterOrDigit
\x77   IsLower
\x78   IsLetter
\x78   IsLetterOrDigit
\x78   IsLower
\x79   IsLetter
\x79   IsLetterOrDigit
\x79   IsLower
\x7A   IsLetter
\x7A   IsLetterOrDigit
\x7A   IsLower
\x7F   IsControl

Char: IsPunctuation, IsSeparator, IsSymbol, IsUpper, IsWhiteSpace

Public Class Tester
    Public Shared Sub Main
        Dim result As New System.Text.StringBuilder
        Dim counter As Integer
        Dim testChar As Char
        Dim testHex As String
        For counter = 0 To 127
            testChar = Chr(counter)
            testHex = "\x" & Hex(counter)
            If Char.IsPunctuation(testChar) Then _
               result.AppendLine(testHex & "   IsPunctuation")
            If Char.IsSeparator(testChar) Then _
               result.AppendLine(testHex & "   IsSeparator")
            If Char.IsSymbol(testChar) Then _
               result.AppendLine(testHex & "   IsSymbol")
            If Char.IsUpper(testChar) Then _
               result.AppendLine(testHex & "   IsUpper")
            If Char.IsWhiteSpace(testChar) Then _
               result.AppendLine(testHex & "   IsWhiteSpace")
        Next counter
        Console.WriteLine(result)
    End Sub
End Class
\x9   IsWhiteSpace
\xA   IsWhiteSpace
\xB   IsWhiteSpace
\xC   IsWhiteSpace
\xD   IsWhiteSpace
\x20   IsSeparator
\x20   IsWhiteSpace
\x21   IsPunctuation
\x22   IsPunctuation
\x23   IsPunctuation
\x24   IsSymbol
\x25   IsPunctuation
\x26   IsPunctuation
\x27   IsPunctuation
\x28   IsPunctuation
\x29   IsPunctuation
\x2A   IsPunctuation
\x2B   IsSymbol
\x2C   IsPunctuation
\x2D   IsPunctuation
\x2E   IsPunctuation
\x2F   IsPunctuation
\x3A   IsPunctuation
\x3B   IsPunctuation
\x3C   IsSymbol
\x3D   IsSymbol
\x3E   IsSymbol
\x3F   IsPunctuation
\x40   IsPunctuation
\x41   IsUpper
\x42   IsUpper
\x43   IsUpper
\x44   IsUpper
\x45   IsUpper
\x46   IsUpper
\x47   IsUpper
\x48   IsUpper
\x49   IsUpper
\x4A   IsUpper
\x4B   IsUpper
\x4C   IsUpper
\x4D   IsUpper
\x4E   IsUpper
\x4F   IsUpper
\x50   IsUpper
\x51   IsUpper
\x52   IsUpper
\x53   IsUpper
\x54   IsUpper
\x55   IsUpper
\x56   IsUpper
\x57   IsUpper
\x58   IsUpper
\x59   IsUpper
\x5A   IsUpper
\x5B   IsPunctuation
\x5C   IsPunctuation
\x5D   IsPunctuation
\x5E   IsSymbol
\x5F   IsPunctuation
\x60   IsSymbol
\x7B   IsPunctuation
\x7C   IsSymbol
\x7D   IsPunctuation
\x7E   IsSymbol

Char.ToUpper

public class Test
   public Shared Sub Main
      Console.WriteLine(Char.ToUpper("a"))
   End Sub
End class
A

ChrW

Module Module1
    Sub Main()
        Dim strR As String
        strR = ChrW("123")
        Console.WriteLine(strR)
    End Sub
End Module
{

Passing Char values to Val

Option Strict On
Public Module Tester
   Public Sub Main()
      " 
      Console.WriteLine(Val("c"c))                     
      Console.WriteLine(Val("1"c))                     
      Console.WriteLine(Val("-"c))                     
      Console.WriteLine(Val("9"c))                     
      Console.WriteLine()
   End Sub
End Module
0
1
0
9

Remove white space in a string

Public Class Tester
    Public Shared Sub Main
        Dim source As String = _
            Space(17) & "This    string    had " & Chr(12) & _
            StrDup(5, Chr(9)) & "extra whitespace. " & Space(27)
        Dim thisIsWhiteSpace As Boolean
        Dim prevIsWhiteSpace As Boolean
        Dim result As New System.Text.StringBuilder(source.Length)
        Dim counter As Integer
        For counter = 0 To source.Length - 1
            prevIsWhiteSpace = thisIsWhiteSpace
            thisIsWhiteSpace = Char.IsWhiteSpace(source.Chars(counter))
            If (thisIsWhiteSpace = False) Then
                If (prevIsWhiteSpace = True) AndAlso _
                   (result.Length > 0) Then result.Append(Space(1))
                result.Append(source.Chars(counter))
            End If
        Next counter
        Console.WriteLine(result.ToString())
    End Sub

End Class
This string had extra whitespace.