当前位置: 首页>>代码示例>>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;未经允许,请勿转载。