|
-
Feb 14th, 2003, 02:27 PM
#1
Thread Starter
Registered User
Split string to array [SOLVED]
How can i turn this string into a array??
"{something with space} oneword next item {and so on}"
Everything inside {} shall be in the same value.
It shall be like:
Array(0) shall be "something with space"
Array(1) shall be "oneword"
Array(2) shall be "next"
Array(3) shall be "item"
Array(4) shall be "and so on"
I hope you understand how i want it.
/Rickard
Last edited by greyhound; Feb 14th, 2003 at 03:30 PM.
-
Feb 14th, 2003, 02:32 PM
#2
well the way you want to do it.. you would have to do extra parsing using some of the following functions
Split()
Mid()
Left()
Right()
InStr()
Split is a function that will create an array from a string.. but you can only specify one thing for it to split on... so you could split on spaces.. but then it wouldn't keep words in the {} together...
-
Feb 14th, 2003, 02:32 PM
#3
There are a couple of ways that come to mind.... one is fairly simple, the other uses brute force.
The brute force method: Use a loop & temp variables to search for { & } and extract things inbetween....
The simpler way: Use the Replace function to change all { &} to something else-- like a pipe "|" or something not likely to appear in the text. Then once replaced, use the Split function to split the text into an array.
VB Code:
Dim strArrayData() As String
Dim strData As String
strArrayData = Split(Replace(Replace(strData,"}","|"), "{","|"), "|")
-
Feb 14th, 2003, 02:39 PM
#4
Originally posted by techgnome
VB Code:
Dim strArrayData() As String
Dim strData As String
strArrayData = Split(Replace(Replace(strData,"}","|"), "{","|"), "|")
will that account for getting the words not in {}??? i don't think it would.. but i didn't run it
-
Feb 14th, 2003, 03:22 PM
#5
Frenzied Member
I think Kleinma has it right. You'll have to extract the values in {} somehow first. I can think of other ways, but they're not necessarily simpler.
You could split based on spaces, then go back & concatenate or join array elements between { and }. Or, if possible, insert a different character, non-printing if need be, between the elements when creating the original file, and split on that. This would make it easy, if it doesn't mess with some other part of your code.
-
Feb 14th, 2003, 03:30 PM
#6
Thread Starter
Registered User
Well i got a for working now... i had a BIG bug in it before thats why i never got it to work 
Code:
Dim strArrayData(0 To 4) As String
Dim strData As String
Dim tmpChr As String
Dim Same As Boolean
Dim Value As Integer
Dim i As Integer
strData = "{something with space} oneword next item {and so on}"
For i = 1 To Len(strData)
tmpChr = Mid(strData, i, 1)
If tmpChr = " " Then
If Same Then
strArrayData(Value) = strArrayData(Value) + tmpChr
Else
Value = Value + 1
End If
ElseIf tmpChr = "{" Then
Same = True
ElseIf tmpChr = "}" Then
Same = False
Else
strArrayData(Value) = strArrayData(Value) + tmpChr
End If
Next i
-
Feb 14th, 2003, 06:47 PM
#7
Frenzied Member
Uh, where do you initially set the value of Same? You test for it before you set it's value. I don't know if booleans default to a value, but you can't rely on that value with different test data sets.
What if your string is "Foo {something with space} oneword next item {and so on}"? Where's your initial read for If Same then?
I haven't run this, maybe it would work, but it looks wrong in theory. Test your program with both good and bad data. Don't rely on default values.
-
Feb 15th, 2003, 01:16 AM
#8
Thread Starter
Registered User
It works as it is now, but your right that it is a good thing to set the values to what i want them to be before it starts.
"The default value of Boolean is False." says the help files.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|