Results 1 to 17 of 17

Thread: about split function

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2002
    Location
    Philippines
    Posts
    877

    about split function

    hi, im trying to split some text file from Text1.Text (Multiline)

    from a text1.text it appears like this

    Location 1
    CENTER: 12345
    REMOTE: +1234
    MESSAGE: OKAY
    what im trying to do is get the REMOTE: so it will appear on my text1.text

    +1234
    because what i did is the Text1.text(multiline) = false

    and my code

    VB Code:
    1. text1.text = split(text1.text,"+",,vbTextCompare) (1)

  2. #2
    Lively Member
    Join Date
    Jul 2006
    Posts
    110

    Re: about split function

    Ummm... I don't get it at all. Could you be a bit clearer about what it is your doing and what your problem is?

  3. #3

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: about split function

    I hate SPLIT() - it's got a purpose...

    but it's abused...

    SPLIT is for taking something that has regular delimiters - maybe commas or slashes or CARRIAGE RETURNS - and splitting that line into an array.

    It certainly is not for finding a value after the word REMOTE: that happens to have a + in front of it.

    Use INSTR - find the position of the word REMOTE:

    Then use INSTR again to find the CRLF that follows the value just found.

    Then use MID to cut out the piece after the word REMOTE: but only up to the CRLF.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: about split function

    Try something like this instead...

    VB Code:
    1. Option Explicit
    2.     Public i As Long
    3.     Public j As Long
    4.    
    5.     Public txt As String
    6.     Public char As String
    7.    
    8.     Public searched As String
    9.     Public result As String
    10.    
    11. Private Sub Command1_Click()
    12.  
    13.     txt = Text1.text
    14.     searched = "REMOTE: "
    15.     result = ""
    16.    
    17.         For i = 1 To Len(txt)
    18.             char = Mid(txt, i, 1)
    19.                 If char = Left(searched, 1) Then
    20.                     If Mid(txt, i, Len(searched)) = searched Then
    21.                         For j = i + Len(searched) To Len(txt)
    22.                             char = Mid(txt, j, 1)
    23.                                 If char = vbLf Then
    24.                                     MsgBox result
    25.                                         Exit Sub
    26.                                 Else
    27.                                     result = result & Mid(txt, j, 1)
    28.                                 End If
    29.                         Next j
    30.                     End If
    31.                 End If
    32.         Next i
    33.             MsgBox "Nothing found!"
    34.  
    35. End Sub

    It works for me

  6. #6
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    Re: about split function

    I completely agree with szlamany.
    Here's much simpler code that you can use
    VB Code:
    1. Dim strtPt As Long
    2. Dim endPt As Long
    3. strtPt = InStr(Text1.Text, "+") + 1
    4. endPt = InStr(strtPt, Text1.Text, vbNewLine) - strtPt
    5. Debug.Print Mid$(Text1.Text, strtPt, endPt)
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

  7. #7

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: about split function

    He's got a multi-line textbox with the word REMOTE: sitting on one of the lines.

    He wants the value after the word REMOTE: - up to the next CRLF.

    The correct method is exactly what shirazamod posted.

    Using SPLIT() to find this involved creating an array - not knowing what line in the array the REMOTE: item was involved with (remember it's a multi-line textbox). Plus SPLIT() is basically doing what shirazamod posted internally but repeatedly for creating an array.

    SPLIT() has it's place but it's certainly not for this one (in my opinion )

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: about split function

    Quote Originally Posted by szlamany
    He's got a multi-line textbox with the word REMOTE: sitting on one of the lines.

    He wants the value after the word REMOTE: - up to the next CRLF.

    The correct method is exactly what shirazamod posted.

    Using SPLIT() to find this involved creating an array - not knowing what line in the array the REMOTE: item was involved with (remember it's a multi-line textbox). Plus SPLIT() is basically doing what shirazamod posted internally but repeatedly for creating an array.

    SPLIT() has it's place but it's certainly not for this one (in my opinion )
    You're right (you both are). I missed the fact that the source is a multi-line textbox.

  10. #10
    Addicted Member
    Join Date
    Jun 2006
    Posts
    172

    Re: about split function

    Assuming Text1.Text has..

    VB Code:
    1. Location 1
    2. CENTER: 12345
    3. REMOTE: +1234
    4. MESSAGE: OKAY

    Then something along the lines of..

    VB Code:
    1. Dim posNum As Integer
    2. Dim spaceNum As Integer
    3. Dim newStr as String
    4.  
    5. posNum = Instr(1, Text1.Text, "+", vbTextCompare)
    6. spaceNum = Instr(posNum, Text1.Text, " ", vbTextCompare)
    7.  
    8. newStr = Mid(Text1.Text, posNum, spaceNum - 1)

    newStr will equal +1234
    • If you found my post to be helpful, please rate me.

  11. #11
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    Re: about split function

    Yeah, that's pretty much what I said
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

  12. #12
    Frenzied Member
    Join Date
    Oct 2003
    Posts
    1,301

    Re: about split function

    Quote Originally Posted by szlamany
    The correct method is exactly what shirazamod posted.
    His method looks for a +.
    Therefore it would work only if the value of remote always begins with a + and no other value ever begins with a +.

    If both of these conditions are true then the method works.

    If not then it would be better to look for "REMOTE:", retrieve the rest of the line and trim away the spaces.

    This also adds the benefit of being able to use the same code to retrieve the values of other fields.
    You would only need to change the searchstring.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2002
    Location
    Philippines
    Posts
    877

    Re: about split function

    hi, thanks for the reply i test the code it works fine but im having problem
    this is the actual text what im reading

    Location 1, folder "Inbox", SIM memory, Inbox folder
    SMS message
    SMSC number : "+639170000119"
    Sent : Friday, July 28, 2006 1:35:52 PM +0800
    Coding : Default GSM alphabet (no compression)
    Remote number : "+639150001234"
    Status : UnRead

    Hello World
    i want the +639170000119 appear on Text1.text and +639150001234 appear in Text2.text

  14. #14
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    Re: about split function

    Then just change the code to look for SMSC number : and then to look for Remote number : and then use Replace() to remove Chr(34)
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2002
    Location
    Philippines
    Posts
    877

    Re: about split function

    @shirazamod

    Sir, what do you mean?

  16. #16
    PowerPoster
    Join Date
    May 2006
    Posts
    2,988

    Re: about split function

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim sLine() As String, sTitle As String, sValue As String
    3.     Dim x As Integer, y As Integer, z As Integer
    4.     sLine = Split(Text1, vbCrLf)
    5.     For x = 0 To UBound(sLine)
    6.         y = InStr(sLine(x), ":")
    7.         z = InStrRev(sLine(x), ":")
    8.         If y Then
    9.             sTitle = Trim$(Left$(sLine(x), y - 1))
    10.             sValue = Right$(sLine(x), Len(sLine(x)) - z)
    11.             sValue = Trim$(Replace(sValue, """", ""))
    12.             If sTitle = "SMSC number" Then
    13.                 Text1 = sValue
    14.             ElseIf sTitle = "Remote number" Then
    15.                 Text2 = sValue
    16.             End If
    17.         End If
    18.     Next x
    19. End Sub

    OR ..

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim sLine() As String, x As Integer
    3.     Dim sTitle As String, sValue As String
    4.     sLine = Split(Text1, vbCrLf)
    5.     For x = 0 To UBound(sLine)
    6.         Call GetLine(sLine(x), ":", sTitle, sValue)
    7.         If sTitle = "SMSC number" Then
    8.             Text1 = sValue
    9.         ElseIf sTitle = "Remote number" Then
    10.             Text2 = sValue
    11.         End If
    12.     Next x
    13. End Sub
    14.  
    15. Private Sub GetLine(ByVal str As String, ByVal val As String, _
    16.     ByRef sTitle As String, ByRef sValue As String)
    17.     Dim y As Integer, z As Integer
    18.     y = InStr(1, str, val, 3): z = InStrRev(str, val, , 3)
    19.     If y Then
    20.         sTitle = Trim$(Left$(str, y - 1))
    21.         sValue = Right$(str, Len(str) - z)
    22.         sValue = Trim$(Replace(sValue, """", ""))
    23.     End If
    24. End Sub

    OR ..

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim sLine() As String, x As Integer
    3.     Dim sTitle As String, sValue As String
    4.     sLine = Split(Text1, vbCrLf)
    5.     For x = 0 To UBound(sLine)
    6.         sTitle = GetTitle(sLine(x), ":")
    7.         sValue = GetValue(sLine(x), ":")
    8.         If sTitle = "SMSC number" Then
    9.             Text1 = sValue
    10.         ElseIf sTitle = "Remote number" Then
    11.             Text2 = sValue
    12.         End If
    13.     Next x
    14. End Sub
    15.  
    16. Private Function GetTitle(ByVal str As String, ByVal val As String) As String
    17.     Dim i As Integer: i = InStr(1, str, val, vbTextCompare)
    18.     If i Then GetTitle = Trim$(Left$(str, i - 1))
    19. End Function
    20.  
    21. Private Function GetValue(ByVal str As String, ByVal val As String) As String
    22.     Dim i As Integer: i = InStrRev(str, val, , vbTextCompare)
    23.     If i Then GetValue = Trim$(Replace(Right$(str, Len(str) - i), """", ""))
    24. End Function
    Last edited by rory; Jul 28th, 2006 at 02:32 AM.

  17. #17
    Fanatic Member
    Join Date
    Aug 2005
    Location
    South Africa
    Posts
    760

    Re: about split function

    Or even simpler
    VB Code:
    1. Dim strtPt As Long
    2. Dim endPt As Long
    3. strtPt = InStr(Text1.Text, "SMSC number : ") + 14
    4. endPt = InStr(strtPt, Text1.Text, vbNewLine) - strtPt
    5. Text1 = Replace$(Mid$(Text1.Text, strtPt, endPt), Chr(34), vbNullString)
    6.  
    7. strtPt = InStr(Text1.Text, "Remote number : ") + 16
    8. endPt = InStr(strtPt, Text1.Text, vbNewLine) - strtPt
    9. Text2 = Replace$(Mid$(Text1.Text, strtPt, endPt), Chr(34), vbNullString)
    If I helped you out, please consider adding to my reputation!

    -- "The faulty interface lies between the chair and the keyboard" --

    VB6 Programs By Me:
    ** Dictionary, Thesaurus & Rhyme-Generator In One ** WMP Recent Files List Editor ** Pretty Impressive Clock ** Extract Firefox History **

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