-
April 29th, 2004, 11:08 PM
#1
Split string using tab,space as delimeter
I read a txt file in a variable and split it in array. Field seperator used in my file is Tab or space (space can be one or more). when i split it by using delimeter TAB then it skips space. pls tell me how can it read space (any number of space)and Tab.
example of txt file
1 0 100 100
2 101 175 75
3 176 250 75
4 251 500 250
-
April 30th, 2004, 12:04 AM
#2
Use regex
in vb 6, SPLIT function does not allow regular expression as a delimiter, but may be you can try this trick
- use regular expression to replace the space(es) or tab(s) to a single character. for example ":".
- next you can use split function to split the result string with delimiter ":".
NOTE : i see that your text example doesn't contain ":" char, so you can do the splitting easily, but if your text file can contain any character (not only number, "+","-",",") you should implement more advance technique.
-Happy coding-
-
April 30th, 2004, 10:13 AM
#3
Here is a hack that splits the string base on a space, then splits the splits based on a Tab and puts the whole thing in an array
kevin
Code:
Private Sub Form_Load()
Dim c()
s = "1 2" & vbTab & _
"3 4" & vbTab & _
"5 6" & vbTab & _
"7 8" & vbTab & _
"9 0" & vbTab & "end"
a = Split(s, " ")
For i = 0 To UBound(a)
b = Split(a(i), vbTab)
For j = 0 To UBound(b)
ReDim Preserve c(k)
c(k) = b(j)
k = k + 1
Next j
Next i
' the split values should now be in the c() array
End Sub
Thank you for your support.
-Bartyles & James (circa 1980)
If it helps, rate it
if it doesn't, well....I tried
Check out Windows Embedded XP. It's XP compontnetized: add only the components you need to build an OS
And in knowing that you know nothing, that makes you the smartest of all.
- Socrates (469 BC - 399 BC)
-
April 30th, 2004, 12:43 PM
#4
Originally posted by kebo
Here is a hack that splits the string base on a space, then splits the splits based on a Tab and puts the whole thing in an array
kevin
Code:
Private Sub Form_Load()
Dim c()
s = "1 2" & vbTab & _
"3 4" & vbTab & _
"5 6" & vbTab & _
"7 8" & vbTab & _
"9 0" & vbTab & "end"
a = Split(s, " ")
For i = 0 To UBound(a)
b = Split(a(i), vbTab)
For j = 0 To UBound(b)
ReDim Preserve c(k)
c(k) = b(j)
k = k + 1
Next j
Next i
' the split values should now be in the c() array
End Sub
all that redim preserving might make the program wild for memory use or very slow...
I vote the first suggestion to be very good.. replace every occurrence of space with something that is not in the file..
like maybe Chr$(0)
then replace every occurrence of tab, with that same Chr$(0)
Then split the string based on Chr$(0)
-
May 1st, 2004, 02:42 AM
#5
I used regular expression for replace space , tab and it is working fine. Code posted by kebo is also working fine. I decided to use first method.
Thanks a lot all of u for given your precious time.
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
|