dcsimg
LinkedIn
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: VB4,VB5,VB6
Expertise: Intermediate
Apr 26, 1999

Process string characters using Byte arrays

When you need to process each character in a string, you can assign the string to a byte array and iterate on its elements (remember: each Unicode character corresponds to two bytes). This approach is usually much faster because it saves a lot of Mid$ functions and, hence, a lot of temporary strings. Here is a very fast way to count the spaces in a string:

Dim b() as Byte, count As Integer
b() = source$
For i = 0 to UBound(b) Step LenB("A")
    If b(i) = 32 Then count = count + 1
Next
Note the unorthodox usage of the LenB() function: it returns 2 under VB4/32, VB5 and VB6, and 1 under VB4/16, so that you can use the same code fragment without the need for a #If compiler directive.

Beware: this technique might not work if you then localize your code for foreign countries that make use of the full Unicode character set.

Francesco Balena
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date