Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through a string and check the byte value of every character?

Code I have:

cell_val = CStr(Nz(fld.value, ""))
Dim iter As Long
For iter = 0 To Len(cell_val) - 1 Step 1
    If Asc(Mid(cell_val, iter, 1)) > 127 Then
        addlog "Export contains ascii character > 127"
    End If
Next iter

This code doesn't work. Anyone know how to do this? I've simply got no idea with VB or VBA.

like image 692
bfabry Avatar asked Sep 17 '08 06:09

bfabry


People also ask

How do I search all characters in a string C++?

Program to loop on every character in string in C++ To loop on each character, we can use loops starting from 0 to (string length – 1). For accessing the character we can either use subscript operator "[ ]" or at() function of string object.

What type of a structure is the best way to iterate through the characters of a string python?

A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).


2 Answers

I believe your problem is that in VBA string indexes start at 1 and not at 0. Try the following:

For iter = 1 To Len(cell_val) 
    If Asc(Mid(cell_val, iter, 1)) > 127 Then
        addlog "Export contains ascii character > 127"
    End If
Next
like image 70
jan.vdbergh Avatar answered Sep 24 '22 20:09

jan.vdbergh


With VBA, VB6 you can just declare a byte array and assign a string value to it and it will be converted for you. Then you can just iterate through it like a regular array.

e.g.

Dim b() as byte
Dim iter As Long
b = CStr(Nz(fld.value, ""))

For iter = 0 To UBound(b)
    if b(iter) > 127 then
        addlog "Export contains ascii character > 127"
    end if
next
like image 22
Sam Avatar answered Sep 26 '22 20:09

Sam