Results 1 to 8 of 8

Thread: Splitting a string...hellp! :)~

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    11

    Red face Splitting a string...hellp! :)~

    Dear VB Elite,

    I have a string with the contents "0001, Smith, Jessica, F, 9/16/1982"

    How can I split the string up where the commas are and store each entry (0001, Smith, etc) in a different string.

    EX: I want it to split the string "0001, Smith, Jessica, F, 9/16/1982" into 5 new string of "00001" and "Smith" and "Jessica" and "F" and "9/16/1982"

    I am doing this with one big text file and I need to do that same thing for each line.

    Thank you so much for reading this post!!

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    sample for you

    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim arrData() As String
    3.     Dim s As String
    4.     Dim i As Integer
    5.    
    6.     Open "c:\c1_1801t113036_testinga45minuterun.txt" For Input As #1
    7.     Do While Not EOF(1)
    8.         Line Input #1, s
    9.         arrData = Split(s, ",")
    10.         For i = 0 To UBound(arrData)
    11.             Debug.Print arrData(i)
    12.         Next i
    13.     Loop
    14.     Close #1
    15. End Sub
    -= a peet post =-

  3. #3
    Frenzied Member Buzby's Avatar
    Join Date
    Jan 1999
    Location
    UK
    Posts
    1,670
    Use the SPLIT command:

    VB Code:
    1. Dim SplitString() as String
    2. Dim ThisString as String
    3.  
    4. ThisString="0001, Smith, Jessica, F, 9/16/1982"
    5.  
    6. SplitString = Split(ThisString,",")

    The array SplitString will now contain the individual parts of the string - eg

    SplitString(0) will contain "0001"
    SplitString(1) will contain "Smith"
    etc
    etc
    'Buzby'
    Visual Basic Developer
    "I'm moving to Theory. Everything works there."

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    my advice... do it like this
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim MyString As String
    3. Dim strArray() As String
    4.  
    5. MyString = "0001, Smith, Jessica, F, 9/16/1982"
    6.  
    7. strArray = Split(MyString, ", ")
    8.  
    9. MsgBox strArray(0) & vbCrLf & strArray(1) & vbCrLf & strArray(2) & vbCrLf & strArray(3) & vbCrLf & strArray(4)
    10. End Sub

    splitting using ", " with the space after the comma.. will leave the space out of the data you read in.. otherwise use the trim function on the values you read in... but i think it is easier to do it this way

  5. #5
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    You guys forgot one thing. Its splitting the commas, not the commas AND the spaces. So "Smith" would come out " Smith".
    VB Code:
    1. Dim Splitted() As String, Expression As String, Delim As String
    2. Expression = "0001, Smith, Jessica, F, 9/16/1982"
    3. Delim = ", " '<-- Notice a space after the comma.
    4. Splitted = Split(Expression, Delim)
    DUH!
    Luke

  6. #6
    Frenzied Member macai's Avatar
    Join Date
    Jul 2001
    Location
    Napanoch NY
    Posts
    1,228
    Originally posted by kleinma
    my advice... do it like this
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim MyString As String
    3. Dim strArray() As String
    4.  
    5. MyString = "0001, Smith, Jessica, F, 9/16/1982"
    6.  
    7. strArray = Split(MyString, ", ")
    8.  
    9. MsgBox strArray(0) & vbCrLf & strArray(1) & vbCrLf & strArray(2) & vbCrLf & strArray(3) & vbCrLf & strArray(4)
    10. End Sub

    splitting using ", " with the space after the comma.. will leave the space out of the data you read in.. otherwise use the trim function on the values you read in... but i think it is easier to do it this way
    aww you got it right before i did!
    Luke

  7. #7

    Thread Starter
    New Member
    Join Date
    Jun 2002
    Posts
    11

    Thumbs up

    Thank you all for those *extremely fast* replies.


  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373
    Originally posted by macai
    aww you got it right before i did!

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