[RESOLVED] quick string question
hi guys, quick question...
if i read the line of a file into a string and then want to extract values from it, is there a string handling command that can skip over white space? so if say my string was as follows
with say 5 or 6 spaces between each character (although not always so i can't just search for the group of spaces), whats the easiest way to read them into 3 variables or an array of variables.
Oh, and i'm coding in VB 6!
thanks in advance and to gavio for answering a similar question earlier!
Ta.
Dave
Re: quick string question
use the Mid$ function to go through the string and pick out the characters that are not spaces, then use a counter variable to store them into the next member of a control array.
Re: quick string question
There numerous ways to accomplish what you need so here is just one of them:
VB Code:
Private Sub Command1_Click()
Dim sFile$, sLine$, i%, arValues() As String
sFile = "c:\test.txt"
Open sFile For Input As #1
Do While Not EOF(1)
Line Input #1, sLine
arValues() = Split(sLine, Space(1))
For i = 0 To UBound(arValues)
If Not Trim(arValues(i)) = "" Then
Debug.Print arValues(i)
End If
Next i
Loop
Close #1
End Sub
Re: quick string question
Assuming the numbers are only single digits, this code will do what you want:
VB Code:
Private Sub Command1_Click()
Dim myArray() As Integer
Dim ArrayCount As Integer
Dim counter As Integer
Dim tempstr As String
ArrayCount = 0
For counter = 1 To Len(Text1.Text)
If Mid$(Text1.Text, counter, 1) <> " " Then
ReDim Preserve myArray(ArrayCount)
myArray(ArrayCount) = Val(Mid$(Text1.Text, counter, 1))
ArrayCount = ArrayCount + 1
End If
Next counter
tempstr = "Values:" & vbNewLine
For counter = 0 To ArrayCount - 1
tempstr = tempstr & myArray(counter) & vbNewLine
Next counter
MsgBox tempstr
End Sub
Text1 is where your string is inputted, and Command1 picks it apart and returns a message box telling you what the numbers in your string are
Re: quick string question
Quote:
Originally Posted by timeshifter
use the Mid$ function to go through the string and pick out the characters that are not spaces, then use a counter variable to store them into the next member of a control array.
Too much work. Just eliminate the spaces all together, first and then count them up.
Try this:
VB Code:
Dim numVals As Integer
Dim numSum As Integer
Dim strStrng As String
Dim i As Integer
strStrng = TextBox1.Text
numVals = Val(strStrng)
numSum = 0
For i = 1 To Len(numVals) + 1
numSum = Val(numSum) + (Val(Mid$(numVals, i, 1)))
Next i
MsgBox numSum
This will work fine if all you're using is numbers and spaces. Otherwise, you'd have to trim out everything that isn't a number using either Mid$ or Replace$
Re: quick string question
Quote:
Originally Posted by dr_dj
whats the easiest way to read them into 3 variables or an array of variables.
We're not looking to add them up, but to put them in an array, which my code does.
Re: quick string question
cheers for the quick reply guys..
i just posted that table as an example.. proabbly best to give an extract from the real file...
HTML Code:
some desctiption of what the file is
0 1.3472 -1.
1 1.2956 -0.9221
2 1.2479 -0.848
3 1.2039 -0.7777
4 1.1628 -0.7109
and goes from 1 - 100 so not always single characters.. will the above methods work equally well for this? i'll use a IsNumeric or whatnot to ignore the first line then go for the data
thanks again
dave
Re: quick string question
This method may be a little more complicated, but it would work equally well. Replace any two-space strings with a single space, and run that loop a few times to reduce all the white space into one space. Then split the resulting string at the space character, and it will store just the numbers into an array. You will need to add a few lines to convert from the string values to Double's, but it's not very difficult to do.
EDIT
Better yet, for that loop, just use a Do loop..
VB Code:
Do While InStr(myText, " ") >0 'looks for two-space characters
myText = Replace( myText, " ", " ") 'repaces any two-space characters with one space
Loop
'when this loop exits, there will only be one space in place of all that white space
Re: quick string question
Quote:
Originally Posted by timeshifter
We're not looking to add them up, but to put them in an array, which my code does.
I see, I overlooked that. However, your code does not fit his needs as his numbers are more than single-digit.
Re: quick string question
Keep looking... my last post has a method to get all of the numbers...
Re: quick string question
Quote:
Originally Posted by timeshifter
This method may be a little more complicated, but it would work equally well. Replace any two-space strings with a single space, and run that loop a few times to reduce all the white space into one space. Then split the resulting string at the space character, and it will store just the numbers into an array. You will need to add a few lines to convert from the string values to Double's, but it's not very difficult to do.
EDIT
Better yet, for that loop, just use a Do loop..
VB Code:
Do While InStr(myText, " ") >0 'looks for two-space characters
myText = Replace( myText, " ", " ") 'repaces any two-space characters with one space
Loop
'when this loop exits, there will only be one space in place of all that white space
Now THAT will work. With a little extra code, anway.
Re: quick string question
@dr_dj:
have you tried my sample yet? (post #3)
Re: quick string question
Here's the rest of it...
VB Code:
Private Sub Command1_Click()
Dim myArray() As String
Dim ArrayCount As Integer
Dim counter As Integer
Dim tempstr As String
Dim myText As String
myText = Text1.Text
Do While InStr(myText, " ") > 0 'looks for two-space characters
myText = Replace(myText, " ", " ") 'repaces any two-space characters with one space
Loop
'when this loop exits, there will only be one space in place of all that white space
myArray = Split(myText, " ")
tempstr = "Numbers:" & vbNewLine
For counter = 0 To UBound(myArray)
tempstr = tempstr & myArray(counter) & vbNewLine
Next counter
MsgBox tempstr
End Sub
Needs a little bit of cleaning up to get rid of the empty value every fourth entry, but that's easy. I'm sure you can figure out the rest.
Re: quick string question
Rhino, thanks,
you're code works a treat...
I'm trying to use:
VB Code:
If IsNumeric(sline) Then
'.. extract data using above code
End If
to skip over the first line(s) of text (string sline) but the If statement won't validate when it has the line i want to analyse!... does IsNumeric require that all characters be numeric? if this is the case, would it be sensible to make a copy of the original string, then remove all the white space from the copy and then use IsNumeric? I'm sure there must be a better way!
thanks for all your helpful suggestions, i'm converting myself from C/C++ to VB so does anyone know of any good online resources?
Thanks again for your replies, i'm sure they'll all be useful soon if not now.
Regards.
Dave
--> timeshifter
Thanks too, thats quite a neat trick replacing the double spaces with single ones... cheers
Re: quick string question
Ok,
Just done some reading and have discovered why IsNumeric won't work.. becasue the string can't be evaluated to a single number.. so, is there a text equivalent so i can test when the line is Not Alphabetic?
thanks.
Re: quick string question
Quote:
Originally Posted by dr_dj
Ok,
Just done some reading and have discovered why IsNumeric won't work.. becasue the string can't be evaluated to a single number.. so, is there a text equivalent so i can test when the line is Not Alphabetic?
thanks.
try checking it with Val() of it. If it is numeric, it'll return a numeric value. If it's just spaces, it'll return "0".
Re: quick string question
Val is one of THE WORST functions available in VB. Not that it only returns numeric characters until first non-numeric found it also (and this one is even more rediculous) recognizes only "." (dot or point) as decimal separator character. So, if you can avoid it then do yourself a favor... :)