當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET String.GetEnumerator方法代碼示例

本文整理匯總了VB.NET中System.String.GetEnumerator方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET String.GetEnumerator方法的具體用法?VB.NET String.GetEnumerator怎麽用?VB.NET String.GetEnumerator使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.String的用法示例。


在下文中一共展示了String.GetEnumerator方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: Main

Module Example
   Sub Main()
      EnumerateAndDisplay("Test Case")
      EnumerateAndDisplay("This is a sentence.")
      EnumerateAndDisplay("Has" & vbTab & "two" & vbTab & "tabs")
      EnumerateAndDisplay("Two" & vbLf & "new" & vbLf & "lines")
   End Sub 
       
   Sub EnumerateAndDisplay(phrase As String)
      Console.WriteLine("The characters in the string ""{0}"" are:", phrase)
       
      Dim charCount As Integer = 0
      Dim controlChars As Integer = 0
      Dim alphanumeric As Integer = 0
      Dim punctuation As Integer = 0
        
      For Each ch In phrase          
         Console.Write("'{0}' ", If(Not Char.IsControl(ch), ch, 
                                     "0x" + Convert.ToUInt16(ch).ToString("X4")))
         If Char.IsLetterOrDigit(ch) Then 
            alphanumeric += 1
         Else If Char.IsControl(ch) Then 
            controlChars += 1
         Else If Char.IsPunctuation(ch) Then 
            punctuation += 1             
         End If
         CharCount += 1
      Next

      Console.WriteLine()
      Console.WriteLine("   Total characters:        {0,3}", CharCount)
      Console.WriteLine("   Alphanumeric characters: {0,3}", alphanumeric)
      Console.WriteLine("   Punctuation characters:  {0,3}", punctuation)
      Console.WriteLine("   Control Characters:      {0,3}", controlChars)
      Console.WriteLine()
    End Sub 
End Module
開發者ID:VB.NET開發者,項目名稱:System,代碼行數:37,代碼來源:String.GetEnumerator

輸出:

The characters in the string "Test Case" are:
T' 'e' 's' 't' ' ' 'C' 'a' 's' 'e'
Total characters:          9
Alphanumeric characters:   8
Punctuation characters:    0
Control Characters:        0

The characters in the string "This is a sentence." are:
T' 'h' 'i' 's' ' ' 'i' 's' ' ' 'a' ' ' 's' 'e' 'n' 't' 'e' 'n' 'c' 'e' '.'
Total characters:         19
Alphanumeric characters:  15
Punctuation characters:    1
Control Characters:        0

The characters in the string "Has       two     tabs" are:
H' 'a' 's' '0x0009' 't' 'w' 'o' '0x0009' 't' 'a' 'b' 's'
Total characters:         12
Alphanumeric characters:  10
Punctuation characters:    0
Control Characters:        2

The characters in the string "Two
new
lines" are:
T' 'w' 'o' '0x000A' 'n' 'e' 'w' '0x000A' 'l' 'i' 'n' 'e' 's'
Total characters:         13
Alphanumeric characters:  11
Punctuation characters:    0
Control Characters:        2

示例2: Test

public class Test
   public Shared Sub Main
        Dim S As String = "Welcome to VB"
        Dim Enumerator As CharEnumerator = S.GetEnumerator
    
        While (Enumerator.MoveNext())
          Console.WriteLine(Enumerator.Current())
        End While

   End Sub
End class
開發者ID:VB程序員,項目名稱:System,代碼行數:11,代碼來源:String.GetEnumerator


注:本文中的System.String.GetEnumerator方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。