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 John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert String to Array of Chars

Status
Not open for further replies.

adamr99

Programmer
Jun 25, 2001
59
US
Hey, I have a very simple problem that I can't seem to figure out (it has been a long day).

I have a string that I want to convert into an array of strings of length 1 (Chars).

Is there any easy way to do this? Or is there a hard way that I am just not thinking of.

Thanks in advance.

Adam
 
Well, this technique is not easy like a "built-in function" would be, but it's straight-forward.

Let's call the original string strOrig and the array of characters strChar. I will use a zero-based array, but that's your choice.
Code:
    ReDim strChar(0 To Len(strOrig) - 1)
    Dim lngCharPosn As Long '=character position within the original string
    For lngCharPosn = 1 to Len(strOrig)
        strChar(lngCharPosn - 1) = Mid$(strOrig, lngCharPosn, 1)
    Next
 
Just curious, but why do this?

The Array and simple string are (for single character operations at least) are nearly indistinguisable. For operations involving unequal length substrings, replace works reasonably well.

So I'm wondering why anyone needs to gobble up processor time and memory to construct the copy in a trivially different approach?

MichaelRed
m.red@att.net

There is never time to do it right but there is always time to do it over
 
Public Sub StringToArray(strSource As String)
Dim strArray() As String
Dim strTemp As String

strTemp = StrConv(strSource, vbUnicode)
strArray = Split(strTemp, Chr$(0))

End Sub [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
To CCLINT,

Haven't tried you code, but I bet it will work.

Kinda unaware about this line
Code:
strTemp = StrConv(strSource, vbUnicode)
Is this what unicode means, a null char pairs for every printable char?
 
Join(list[, delimiter])
Returns a string created by joining a number of substrings contained in an array




 
SilverSide: The question is to convert a string to an array and not an array to a string.... [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Dim A() As Byte
A = "hello"
MsgBox Chr(A(0)) Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Whoa,

Thanks guys for all of the help.

To answer your question MichaelRed, I needed to separate out a string to multiple text boxes that wasn't delimited.

Thanks!
Adam
 
sunaj:
And yet another method....
I guess it could be used if every other element were to be ignored.
Or, did you mean to impliment this in a different manner?

If the Filter function worked on Byte arrays, then this could be used to remove the unwanted elements. [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
CCLINT
Yes, You would have to use every other element or StrConv as you did.

I guess with the purpose of putting each character (substring?) in different textboxes it would be easier to use Mid$ in a loop of a textbox array... Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top