Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2004
    Location
    South Africa
    Posts
    178

    Question Unanswered: return index to character in string

    I want to return the position of a character in a string looking from the right
    eg find the "\" in c:\Documents\Me
    therefore "\" is at position 13

    thanks in advance
    ActionAnt

  2. #2
    Join Date
    Aug 2003
    Location
    Delft, The Netherlands (EU)
    Posts
    447

    Re: return index to character in string

    I had the same problem, and I found no other solution than to define my own Instr() function.
    Make everything as simple as possible, but not simpler! - A. Einstein
    DB Problems? DB Explorer, BTrieve Re-engineering, DB Conversions & ETL? Conversion Tool

  3. #3
    Join Date
    Feb 2004
    Location
    Dorset UK
    Posts
    147


    MyPath ="c:\Documents\Me"

    Where_is_it = instr(5,MyPath,"\")

    Msgbox Where_is_it



    I used 5 as a start point, change this number from 1 to ... but if you use 1 then it will say 3 for the first \.

    Ken

  4. #4
    Join Date
    Nov 2002
    Location
    The Netherlands
    Posts
    61
    This will goto through the string until it finds the Char you want to find which is the "\" now. Wenn it finds it, it will put the current place in the textfield.

    CharSearched="\"
    For i = Len(mystring) - 1 To 0 Step -1
    If CharSearched = Mid(mystring, i, 1) Then
    Textfield.value = i
    Exit For
    End If
    Next i

    this is a little longer but starts at the back

  5. #5
    Join Date
    Mar 2004
    Location
    Netherlands
    Posts
    100

    Re: return index to character in string

    You can try with

    LOCATE(string_exp1, string_exp2[, start])
    it returns the starting position of the first occurrence of string_exp1 within string_exp2.

  6. #6
    Join Date
    Aug 2003
    Location
    Delft, The Netherlands (EU)
    Posts
    447

    Re: return index to character in string

    Originally posted by pipeio
    LOCATE(string_exp1, string_exp2[, start])
    I don't know a function LOCATE in VBA at all. Which language do you mean?

    Second question: why do you thing that LOCATE is solving the original problem?
    Make everything as simple as possible, but not simpler! - A. Einstein
    DB Problems? DB Explorer, BTrieve Re-engineering, DB Conversions & ETL? Conversion Tool

  7. #7
    Join Date
    Nov 2002
    Location
    The Netherlands
    Posts
    61
    Anything wrong with my code?

    CharSearched="\"
    For i = Len(mystring) - 1 To 0 Step -1
    If CharSearched = Mid(mystring, i, 1) Then
    Textfield.value = i
    Exit For
    End If
    Next i

  8. #8
    Join Date
    Mar 2004
    Posts
    6
    you can use the function below to do this, same as instr() but starts looking from the right instead of the left.

    Syntax : InstrRev(stringcheck, stringmatch[, start[, compare]])

    stringcheck Required. String expression being searched.

    stringmatch Required. String expression being searched for.

    start Optional. Numeric expression that sets the starting position for each search. If omitted, 1 is used, which means that the search begins at the last character position. If start contains
    Null, an error occurs.

    compare Optional. Numeric value indicating the kind of comparison to use when evaluating substrings. If omitted, a binary comparison is performed. See Settings section for values.



    cheers.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •