devxlogo

Process string characters using Byte arrays

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 Integerb() = source$For i = 0 to UBound(b) Step LenB("A")    If b(i) = 32 Then count = count + 1Next

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.

See also  Why ChatGPT Is So Important Today
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist