Results 1 to 8 of 8

Thread: does the INSTR( have a way to you tell you the location of the second delimiter

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    124

    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

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

    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)?

  3. #3
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  4. #4
    Member
    Join Date
    Dec 2007
    Posts
    57

    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)

  5. #5
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    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)

  6. #6
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: does the INSTR( have a way to you tell you the location of the second delimiter

    Quote 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)
    Quote 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.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  7. #7
    Frenzied Member
    Join Date
    Dec 2007
    Posts
    1,072

    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:
    1. strSource = Replace(strSource, Split(strSource, ",")(0), vbNullString)

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2008
    Posts
    124

    Re: does the INSTR( have a way to you tell you the location of the second delimiter

    Nice, Thank You

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