Results 1 to 5 of 5

Thread: VB String Split

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    14

    VB String Split

    Hi,

    I have one String array like this.....

    str(0)=msg1/msg2/msg3/msg4
    str(1)=msg1/msg2/msg5
    str(2)=msg1/msg2/msg6/msg7
    str(3)=msg1/msg2/msg8

    here i want to split the string array....and store like this......
    i want to store msg1/msg2 in one string........after msg2 i want to store another string ...how can i do it.....anybody know about it....plz tell me.....

    thanks in advance...

    kavi_k

  2. #2
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    So would this be correct :

    str(n)=msg0/msg1/msg2/msg3/msg4/...msgn-1/msgn

    You then want to split up as follows :
    msg0/msg1 , msg2/msg3 , msg4/msg5 ... msgn-1/msgn
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Christ I must still be asleep this morning.
    This is taking way longer than it should.

    Anyway, this is almost it :

    VB Code:
    1. Private Function split2(ByVal strString As String, ByVal strDelimeter As String) As String()
    2.     Dim strTemp() As String: strTemp = Split(strString, strDelimeter)
    3.     Dim i As Long, n As Long, arrRetVal() As String: ReDim arrRetVal(UBound(strTemp))
    4.     For i = 2 To UBound(strTemp) + 1 Step 2
    5.         arrRetVal(i - 2) = strTemp(i - 2) & "/" & strTemp(i - 1)
    6.     Next
    7.     split2 = arrRetVal
    8. End Function
    9.  
    10. Private Sub Form_Load()
    11.     Dim x As String, y() As String, i As Long
    12.     x = "a/b/c/d/e/f/g/h"
    13.     y = split2(x, "/")
    14.     For i = 0 To UBound(y)
    15.         Debug.Print y(i)
    16.     Next
    17. End Sub

    That does the splitting the way you want it, but it leaves in a black space in every second index of the array.
    I'll fix that in a few mins.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    End result :

    VB Code:
    1. Private Function split2(ByVal strString As String, ByVal strDelimeter As String) As String()
    2.     Dim strTemp() As String: strTemp = Split(strString, strDelimeter)
    3.     Dim i As Long, n As Long, arrRetVal() As String: ReDim arrRetVal((UBound(strTemp) - 1) / 2)
    4.     For i = 0 To UBound(strTemp) - 1
    5.         arrRetVal(n) = strTemp(i) & "/" & strTemp(i + 1)
    6.         If i Mod 2 = 0 Then
    7.             n = n + 1
    8.         End If
    9.     Next
    10.     split2 = arrRetVal
    11. End Function
    12.  
    13. Private Sub Form_Load()
    14.     Dim x As String, y() As String, i As Long
    15.     x = "a/b/c/d/e/f/g/h"
    16.     y = split2(x, "/")
    17.     For i = 0 To UBound(y)
    18.         Debug.Print y(i)
    19.     Next
    20. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  5. #5
    Addicted Member E-Link's Avatar
    Join Date
    Nov 2001
    Location
    INA
    Posts
    242
    why dont you use split function from VB .

    something like this :

    VB Code:
    1. dim sP() as String
    2.  dim  x as String
    3.  x = "a/b/c/d/"
    4.  sP = Split(x,"/")

    that will result :
    sP(0) = a
    sP(1) = b
    and etc...

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