Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Addressing String as array

Status
Not open for further replies.

yumbelie

Programmer
Dec 31, 2002
96
GB
Hi, Wondering, how can you address a string as an array in Visual Basic? This is mainly because I want to test the contents of a string against an array containing permitted characters. I assumed, that like C, a string could be addressed as an array i.e. blah = "Hello" echo blah(0) --> "H", but apparantly this is not the case. I can it can be done with built in VB for loops, copying the contents etc, but that seems horribly inefficient, and I read that the CopyMemory Win32Api function could be used to perform a significantly more effective conversion, but the article in question was somewhat truncated as to the details.

If someone could provide me with some examples, ideally involving the CopyMemory Api (if that is the most efficient method) - That'd be fantastic.

Thanks.
 
because I want to test the contents of a string against an array containing permitted characters

Perhaps a combination of standard string manipulation functions will suffice:
e.g.
Code:
If Instr(1,blah,checkarray(0))<>0 then 'character in string

To address e.g. especially 3rd character of string:
Code:
third=Mid(blah,3,1)
whereas 3 defines position and 1 the amount of chars to be read.

Would that help you?
Cheers,
MakeItSo

Andreas Galambos
EDP / Technical Support Specialist
(andreas.galambos@bowneglobal.de)
HP:
 
I believe you can use the in-built MID function as such:
Code:
dim cntr as Integer
dim myString as String

string = "Hello"

for cntr = 1 to len(myString)
    debug.print mid(mystring,cntr,1)
next
 
It is also worth knowing that you can copy a string over to an array without using a loop and without using CopyMemory. Consider the following:
Code:
    Dim arrUniWombat() As Byte
    Dim arrANSIWombat() As Byte
    Dim strWombat As String
    
    strWombat = "Hello"
    arrUniWombat = strWombat
    arrANSIWombat = StrConv(strWombat, vbFromUnicode)
 
And last but not least is the Split function which neatly splits the characters of a string in a single call using the Unicode version of the string.

In fact, this code was posted by CCLINT in this forum a long time ago, but I don't remember the thread.
___
[tt]
Private Sub Form_Load()
Dim Blah As String, arrBlah() As String
Blah = "Hello"
arrBlah = Split(StrConv(Blah, vbUnicode), vbNullChar)

Debug.Print arrBlah(0)
Debug.Print arrBlah(1)
Debug.Print arrBlah(2)
Debug.Print arrBlah(3)
Debug.Print arrBlah(4)
End Sub[/tt]
 
Btw does anyone know what happened to CCLINT?????? I think it's been about three months since his/her last post...
 
No, just seems to have vanished. Sad, as he was a good contributor
 
Yeah right.
Let's hope he's just filled with projects and doesn't have the time for TT...
[sadeyes]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top