Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: VBNET
Expertise: beginner
Jan 5, 2002

Iterating over the characters in a string

Visual Basic .NET strings support the For Each statement, so you can iterate over each individual character as follows:

Dim s As String = "ABCDE"
Dim c As Char
For Each c In s
    Console.Write(c & ".")       ' => A.B.C.D.E.
Next
However, you should bear in mind that For Each loops on string characters aren't optimized. The following solution, that uses the Chars property, is 2-5 times faster:

Dim i As Integer
For i = 0 To s.Length - 1
    Console.Write(s.Chars(i) & ".")       ' => A.B.C.D.E.
Next
Francesco Balena
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date