|
-
Aug 8th, 2002, 07:48 AM
#1
Thread Starter
New Member
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!!
-
Aug 8th, 2002, 07:52 AM
#2
-= B u g S l a y e r =-
sample for you
VB Code:
Private Sub Command1_Click()
Dim arrData() As String
Dim s As String
Dim i As Integer
Open "c:\c1_1801t113036_testinga45minuterun.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, s
arrData = Split(s, ",")
For i = 0 To UBound(arrData)
Debug.Print arrData(i)
Next i
Loop
Close #1
End Sub
-
Aug 8th, 2002, 07:53 AM
#3
Frenzied Member
Use the SPLIT command:
VB Code:
Dim SplitString() as String
Dim ThisString as String
ThisString="0001, Smith, Jessica, F, 9/16/1982"
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."
-
Aug 8th, 2002, 07:55 AM
#4
my advice... do it like this
VB Code:
Private Sub Command1_Click()
Dim MyString As String
Dim strArray() As String
MyString = "0001, Smith, Jessica, F, 9/16/1982"
strArray = Split(MyString, ", ")
MsgBox strArray(0) & vbCrLf & strArray(1) & vbCrLf & strArray(2) & vbCrLf & strArray(3) & vbCrLf & strArray(4)
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
-
Aug 8th, 2002, 07:56 AM
#5
Frenzied Member
You guys forgot one thing. Its splitting the commas, not the commas AND the spaces. So "Smith" would come out " Smith".
VB Code:
Dim Splitted() As String, Expression As String, Delim As String
Expression = "0001, Smith, Jessica, F, 9/16/1982"
Delim = ", " '<-- Notice a space after the comma.
Splitted = Split(Expression, Delim)
DUH!
-
Aug 8th, 2002, 07:57 AM
#6
Frenzied Member
Originally posted by kleinma
my advice... do it like this
VB Code:
Private Sub Command1_Click()
Dim MyString As String
Dim strArray() As String
MyString = "0001, Smith, Jessica, F, 9/16/1982"
strArray = Split(MyString, ", ")
MsgBox strArray(0) & vbCrLf & strArray(1) & vbCrLf & strArray(2) & vbCrLf & strArray(3) & vbCrLf & strArray(4)
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!
-
Aug 8th, 2002, 07:58 AM
#7
Thread Starter
New Member
Thank you all for those *extremely fast* replies.
-
Aug 8th, 2002, 07:58 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|