|
-
Jul 1st, 2008, 10:00 PM
#1
Thread Starter
Lively Member
does the INSTR( have a way to you tell you the location of the second delimiter
does the INSTR( have a way to you tell you the location of the second or third item in a string
for example
first,last,address,city,state
I want to grab the text left of the second ,
I know i can do a split on it but I want to skip that
-
Jul 1st, 2008, 10:36 PM
#2
Re: does the INSTR( have a way to you tell you the location of the second delimiter
If you know the position of the first delimiter you can find the second, and if you know the second you can find the third, but what's wrong with using Split (and perhaps Join to put it back together)?
-
Jul 1st, 2008, 10:48 PM
#3
Re: does the INSTR( have a way to you tell you the location of the second delimiter
Try to interpret the CSV as an ADO data source (table in this case), and learn SQL. You then won't need to repetitively perform string manipulation on each row.
-
Jul 2nd, 2008, 05:46 AM
#4
Member
Re: does the INSTR( have a way to you tell you the location of the second delimiter
This works...
Code:
strSource = "first,last,address,city,state"
strFirst = Left$(strSource, InStr(1, strSource, ",") - 1)
strLast = Mid$(strSource, InStr(1, strSource, ",") + 1, InStr(InStr(1, strSource, ",") + 1, strSource, ",") - InStr(1, strSource, ",") - 1)
strAddress = Mid$(strSource, InStr(InStr(1, strSource, ",") + 1, strSource, ",") + 1, InStr(InStr(InStr(1, strSource, ",") + 1, strSource, ",") + 1, strSource, ",") - InStr(InStr(1, strSource, ",") + 1, strSource, ",") - 1)
But this is much easier...
Code:
strSource = "first,last,address,city,state"
strFirst = Split(strSource, ",")(0)
strLast = Split(strSource, ",")(1)
strAddress = Split(strSource, ",")(2)
-
Jul 3rd, 2008, 09:12 PM
#5
Frenzied Member
Re: does the INSTR( have a way to you tell you the location of the second delimiter
Code:
strsource = Replace(strsource, Left(strsource, InStr((InStr(1, strsource, ",") + 1), strsource, ",")), vbNullString)
-
Jul 3rd, 2008, 10:21 PM
#6
Re: does the INSTR( have a way to you tell you the location of the second delimiter
 Originally Posted by reinholdmesner
Code:
strSource = "first,last,address,city,state"
strFirst = Split(strSource, ",")(0)
strLast = Split(strSource, ",")(1)
strAddress = Split(strSource, ",")(2)
That is a good way but do that only when you want to grab only a single part such as you want to grab the "Last" only:
Code:
strLast = Split(strSource, ",")(1)
For more than one parts, you should use a temp var to split just once, because the Split() is quite expensive.
Code:
strSource = "first,last,address,city,state"
arrParts = Split(strSource, ",")
strFirst = arrParts(0)
strLast = arrParts(1)
strAddress = arrParts(2)
 Originally Posted by Zach_VB6
Code:
strsource = Replace(strsource, Left(strsource, InStr((InStr(1, strsource, ",") + 1), strsource, ",")), vbNullString)
The einholdmesner's method is much shorter and simpler.
I have seen your many posts. Combined too many statements into one is not a good practice of coding unless they are simple statements because it will be hard to debug, that makes your brain and eyes works harder to understand the statement.
** If this is a student homework and it requires to use Instr() to find the positions of commas then:
Code:
strSource = "first,last,address,city,state"
i = Instr(strCource, ",")
If i > 0 Then
j = Instr(i+1, strCource, ",")
strLast = Mid$(strSource, i+1, j-i-1)
'Else
'...
End If
Last edited by anhn; Jul 3rd, 2008 at 10:33 PM.
-
Jul 3rd, 2008, 11:41 PM
#7
Frenzied Member
Re: does the INSTR( have a way to you tell you the location of the second delimiter
Here's another way, with not too much crammed into one line:
vb Code:
strSource = Replace(strSource, Split(strSource, ",")(0), vbNullString)
-
Jul 6th, 2008, 04:52 PM
#8
Thread Starter
Lively Member
Re: does the INSTR( have a way to you tell you the location of the second delimiter
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
|