Results 1 to 17 of 17

Thread: [RESOLVED] quick string question

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    9

    Resolved [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

    HTML Code:
    1        4        8
    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

  2. #2
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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.

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: quick string question

    There numerous ways to accomplish what you need so here is just one of them:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim sFile$, sLine$, i%, arValues() As String
    3.  
    4.     sFile = "c:\test.txt"
    5.     Open sFile For Input As #1
    6.         Do While Not EOF(1)
    7.             Line Input #1, sLine
    8.             arValues() = Split(sLine, Space(1))
    9.             For i = 0 To UBound(arValues)
    10.                 If Not Trim(arValues(i)) = "" Then
    11.                     Debug.Print arValues(i)
    12.                 End If
    13.             Next i
    14.         Loop
    15.     Close #1
    16.  
    17. End Sub

  4. #4
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: quick string question

    Assuming the numbers are only single digits, this code will do what you want:

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim myArray() As Integer
    4.     Dim ArrayCount As Integer
    5.     Dim counter As Integer
    6.     Dim tempstr As String
    7.    
    8.     ArrayCount = 0
    9.    
    10.     For counter = 1 To Len(Text1.Text)
    11.         If Mid$(Text1.Text, counter, 1) <> " " Then
    12.             ReDim Preserve myArray(ArrayCount)
    13.             myArray(ArrayCount) = Val(Mid$(Text1.Text, counter, 1))
    14.             ArrayCount = ArrayCount + 1
    15.         End If
    16.     Next counter
    17.    
    18.     tempstr = "Values:" & vbNewLine
    19.    
    20.     For counter = 0 To ArrayCount - 1
    21.         tempstr = tempstr & myArray(counter) & vbNewLine
    22.     Next counter
    23.    
    24.     MsgBox tempstr
    25.    
    26. 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

  5. #5
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    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:
    1. Dim numVals As Integer
    2.     Dim numSum As Integer
    3.     Dim strStrng As String
    4.     Dim i As Integer
    5.    
    6.     strStrng = TextBox1.Text
    7.    
    8.     numVals = Val(strStrng)
    9.     numSum = 0
    10.    
    11.         For i = 1 To Len(numVals) + 1
    12.             numSum = Val(numSum) + (Val(Mid$(numVals, i, 1)))
    13.         Next i
    14.  
    15.     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$

  6. #6
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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.

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    9

    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

  8. #8
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    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:
    1. Do While InStr(myText, "  ") >0 'looks for two-space characters
    2.    myText = Replace( myText, "  ", " ") 'repaces any two-space characters with one space
    3. Loop
    4. 'when this loop exits, there will only be one space in place of all that white space

  9. #9
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    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.

  10. #10
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: quick string question

    Keep looking... my last post has a method to get all of the numbers...

  11. #11
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    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:
    1. Do While InStr(myText, "  ") >0 'looks for two-space characters
    2.    myText = Replace( myText, "  ", " ") 'repaces any two-space characters with one space
    3. Loop
    4. '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.

  12. #12

  13. #13
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465

    Re: quick string question

    Here's the rest of it...

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Dim myArray() As String
    4.     Dim ArrayCount As Integer
    5.     Dim counter As Integer
    6.     Dim tempstr As String
    7.     Dim myText As String
    8.    
    9.     myText = Text1.Text
    10.    
    11.     Do While InStr(myText, "  ") > 0 'looks for two-space characters
    12.        myText = Replace(myText, "  ", " ")  'repaces any two-space characters with one space
    13.     Loop
    14.     'when this loop exits, there will only be one space in place of all that white space
    15.    
    16.     myArray = Split(myText, " ")
    17.    
    18.     tempstr = "Numbers:" & vbNewLine
    19.    
    20.     For counter = 0 To UBound(myArray)
    21.         tempstr = tempstr & myArray(counter) & vbNewLine
    22.     Next counter
    23.    
    24.     MsgBox tempstr
    25.    
    26. 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.

  14. #14

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    9

    Re: quick string question

    Rhino, thanks,

    you're code works a treat...


    I'm trying to use:

    VB Code:
    1. If IsNumeric(sline) Then
    2.   '.. extract data using above code
    3. 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
    Last edited by dr_dj; Oct 30th, 2006 at 04:35 PM.

  15. #15

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    9

    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.

  16. #16
    Hyperactive Member BrendanDavis's Avatar
    Join Date
    Oct 2006
    Location
    Florida
    Posts
    492

    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".

  17. #17
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    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...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width