Results 1 to 11 of 11

Thread: Take part of all lines in a text box

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Portugal
    Posts
    103

    Take part of all lines in a text box

    I have several lines that are like this:

    5:16pm up 9 min(s), 1 user, load average: 0.03, 0.06, 0.04
    6:16pm up 9 min(s), 2 user, load average: 0.03, 0.06, 0.04
    7:16pm up 9 min(s), 3 user, load average: 0.03, 0.06, 0.04

    I just what this part of the line(0.03, 0.06, 0.04)

    Anyone has an idea? I don´t!

  2. #2
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Well

    if you now every line of "5:16pm up 9 min(s), 1 user, load average:" then you could use the VB Replace function
    to replace 5:16pm up 9 min(s), 1 user, load average: with " "
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

  3. #3
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Assuming that there is a CR/LF pair at the end of each line, you could always do something simple like:
    VB Code:
    1. Temp1 = Mid(Text1, 43)
    2.     MsgBox Left(Temp1, 16)
    3.    
    4.     Temp2 = Mid(Temp1, 43 + 16 + 2)
    5.     MsgBox Left(Temp2, 16)
    6.    
    7.     Temp3 = Mid(Temp2, 43 + 16 + 2)
    8.     MsgBox Left(Temp3, 16)
    If you don't like the hard-coded numbers, do a locate for the words "average:" for the starting point, and VBCrLf for the end point.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Portugal
    Posts
    103
    Sorry but the number Cr/LF isn´t always pair, anyone has more ideas?

  5. #5
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    You need to code something like the following algorithm, based around the colons in the string:

    Code:
    String = YourTextBoxString
    
    LOOP, while String Length > 16
      NewString = String, starting at the location of ":" + 1
      IF 1st character of NewString is a space THEN
            UseFullString = Next 16 characters from NewString
      END IF
      String = NewString, starting at location 17
    END LOOP

  6. #6
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    VB Code:
    1. Private Function ReturnLastDigitsOfLine(ByVal strWholeLine As String)
    2.    
    3.     Dim intChrCounter As Integer
    4.     ReturnLastDigitsOfLine = ""
    5.    
    6.     For intChrCounter = 1 To Len(strWholeLine)
    7.    
    8.         If Not IsNumeric(Mid(Right(strWholeLine, intChrCounter), 1, 1)) Then
    9.        
    10.             If Not ((Mid(Right(strWholeLine, intChrCounter), 1, 1)) = "," Or _
    11.             (Mid(Right(strWholeLine, intChrCounter), 1, 1)) = "." Or _
    12.             (Mid(Right(strWholeLine, intChrCounter), 1, 1)) = " ") Then
    13.            
    14.                 ReturnLastDigitsOfLine = Trim(Right(strWholeLine, intChrCounter - 1))
    15.                 Exit For
    16.                
    17.             End If
    18.        
    19.         End If
    20.    
    21.     Next intChrCounter
    22.    
    23. End Function
    Usage:
    VB Code:
    1. Private Sub Form_Load()
    2.     Dim strTemp As String
    3.     strTemp = "5:16pm up 9 min(s), 1 user, load average: 0.03, 0.06, 0.04"
    4.    
    5.     MsgBox ReturnLastDigitsOfLine(strTemp)
    6. End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  7. #7
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    But that only gets the last bit of the string, rather than each item as it appears....

    I understand that the string in the text box looks like this:
    Code:
        Text1 = "5:16pm up 9 min(s), 1 user, load average: 0.03, 0.06, 0.04" & vbCrLf & _
    "6:16pm up 9 min(s), 2 user, load average: 0.03, 0.06, 0.05" & vbCrLf & _
    "7:16pm up 9 min(s), 3 user, load average: 0.03, 0.06, 0.06"
    and so this code will process that string, displaying the end of each line in the message box. It decodes the string based on the ":"
    VB Code:
    1. strString = Text1
    2.     While Len(strString) > 10
    3.         strNewString = Mid(strString, InStr(strString, ":") + 1)
    4.         If Left(strNewString, 1) = " " Then
    5.             MsgBox Mid(strNewString, 2, 16)
    6.         End If
    7.         strString = Mid(strNewString, 17)
    8.     Wend

  8. #8
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    okay now you've lost me completely
    I have several lines that are like this:

    5:16pm up 9 min(s), 1 user, load average: 0.03, 0.06, 0.04

    I just want this part of the line(0.03, 0.06, 0.04)
    Sounds like he wants everything past the last ":" character, no?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  9. #9
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Maybe we will have to see what the original poster says....

    I was also taking the subject of the post: "Take part of all lines in a text box" ....
    meaning that the text box had multiple lines in it.


    If we take it that the text box only has one line in it, then a simple InStrRev looking for the ":" is all that is needed.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Sep 2002
    Location
    Portugal
    Posts
    103
    I still can't get the last part of each line to the text box, i have tried this to take the part that i don't want, but the result is nothing, i don't know what i am doing wrong:

    /
    XP = FrmPrincipal.Text1

    Do While Len(XP) > 10
    XPP = Left(XP, 60)
    If Left(XPP, 1) = " " Then
    MsgBox Mid(XPP, 1, 60)
    XP = Replace(XP, XPP, "")
    End If
    XP = Mid(XPP, 17)
    Loop

    /

    someone has any idea about this?
    thank's in advance

  11. #11
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Newbury, UK
    Posts
    1,878
    Do While Len(XP) > 10
    XPP = Left(XP, 60)

    At this point you only have 60 characters in XPP. You have thrown away the rest of the text box. Is this what you wanted to do??

    If Left(XPP, 1) = " " Then
    MsgBox Mid(XPP, 1, 60)
    XP = Replace(XP, XPP, "")
    End If

    You show the MsgBox ONLY if the first character is a space.
    What did you intend the "Replace" statement to do?

    How about replacing that bit of code with this and see if its what you wanted:
    VB Code:
    1. Do While Len(XP) > 10
    2.         ' find where the colon is in the string
    3.         x = InStr(1, XP, ": ")
    4.         ' grab the rest of the string, after the colon
    5.         XPP = Mid(XP, x + 2)
    6.         ' display the first 17 characters
    7.         MsgBox Left(XPP, 17)
    8.         ' Loop around, with the rest of the string
    9.         XP = Mid(XPP, 17)
    10.     Loop

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