Get last part of a string

Ask a Question related to ASP Database, Design and Development.

  1. #1

    Default Get last part of a string

    Hi All,

    I'm trying to get the last part of a string.
    In VB there's a function called lastinstr() who does the work.
    But it doesn't seem to work in ASP.

    The string I have is for example:
    D:\Marco\ASIC2000\Images\Weapon\leo1schrägmit.jpg
    Now I want to get rit of everything before the last "\" so that
    leaves: leo1schrägmit.jpg

    One of the problems I have is that I never know what the string
    looks like so I always looked for the last instance of "\" and cut
    eveything left of it.

    In VB this code did it:

    Function LastInStr(strSearched As String, strSought As String) As Integer

    Dim intCurrVal As Integer
    Dim intLastPosition As Integer

    intCurrVal = InStr(strSearched, strSought)
    Do Until intCurrVal = 0
    intLastPosition = intCurrVal
    intCurrVal = InStr(intLastPosition + 1, strSearched, strSought)
    Loop
    LastInStr = intLastPosition

    End Function

    Can I use this function in ASP, and Where do I put it.
    Or is there something else I can do?

    Regards
    Marco
    The NEtherlands
    Krechting Guest

  2. Similar Questions and Discussions

    1. numeric part of a string
      Can anyone tell me how can I get the numeric part of a string . for example I'm using this..Select substring('walnut 2224...
    2. Selecting part of a string
      Hello all, How do I Select and put in a variable the url (www.domain.com) of the following string: javascript:openWin("www.domain.com") ...
    3. Parse part of string (mid string function ?)
      Can someone tell me how to parse part of a string? I can use the following: <?php $text = $_SERVER; //$PHP_SELF //(document.url); echo $text;...
    4. RegExp that looks for a string and only replaced part of it...
      I'm after a regular expression that looks through a string for a certain code, and then replaces part of that code with some markup: So look in...
    5. getting part of a string
      Anyone have a sure way of grabbing just the file name from a string that looks like this: C:/Documents and Settings/Bobo the Bugbear/My...
  3. #2

    Default Re: Get last part of a string

    Why is this in the DB group?

    Anyway, you can do this if you are already using FileSystemObject in the
    page:


    set fso = CreateObject("Scripting.FileSystemObject")
    set fs = fso.GetFile("c:\boot.ini")
    response.write fs.name




    "Krechting" <m.krechting@chello.nl> wrote in message
    news:a71c776d.0312281546.1d025920@posting.google.c om...
    > Hi All,
    >
    > I'm trying to get the last part of a string.
    > In VB there's a function called lastinstr() who does the work.
    > But it doesn't seem to work in ASP.
    >
    > The string I have is for example:
    > D:\Marco\ASIC2000\Images\Weapon\leo1schrägmit.jpg
    > Now I want to get rit of everything before the last "\" so that
    > leaves: leo1schrägmit.jpg
    >
    > One of the problems I have is that I never know what the string
    > looks like so I always looked for the last instance of "\" and cut
    > eveything left of it.
    >
    > In VB this code did it:
    >
    > Function LastInStr(strSearched As String, strSought As String) As Integer
    >
    > Dim intCurrVal As Integer
    > Dim intLastPosition As Integer
    >
    > intCurrVal = InStr(strSearched, strSought)
    > Do Until intCurrVal = 0
    > intLastPosition = intCurrVal
    > intCurrVal = InStr(intLastPosition + 1, strSearched, strSought)
    > Loop
    > LastInStr = intLastPosition
    >
    > End Function
    >
    > Can I use this function in ASP, and Where do I put it.
    > Or is there something else I can do?
    >
    > Regards
    > Marco
    > The NEtherlands

    Foo Man Chew Guest

  4. #3

    Default Re: Get last part of a string

    use split() and take the last entry

    e.g.

    urlSplit = split("D:\Marco\ASIC2000\Images\Weapon\leo1schrägm it.jpg","\")
    msgbox urlSplit(UBound(urlSplit))

    regards
    paul

    "Krechting" <m.krechting@chello.nl> wrote in message
    news:a71c776d.0312281546.1d025920@posting.google.c om...
    > Hi All,
    >
    > I'm trying to get the last part of a string.
    > In VB there's a function called lastinstr() who does the work.
    > But it doesn't seem to work in ASP.
    >
    > The string I have is for example:
    > D:\Marco\ASIC2000\Images\Weapon\leo1schrägmit.jpg
    > Now I want to get rit of everything before the last "\" so that
    > leaves: leo1schrägmit.jpg
    >
    > One of the problems I have is that I never know what the string
    > looks like so I always looked for the last instance of "\" and cut
    > eveything left of it.
    >
    > In VB this code did it:
    >
    > Function LastInStr(strSearched As String, strSought As String) As Integer
    >
    > Dim intCurrVal As Integer
    > Dim intLastPosition As Integer
    >
    > intCurrVal = InStr(strSearched, strSought)
    > Do Until intCurrVal = 0
    > intLastPosition = intCurrVal
    > intCurrVal = InStr(intLastPosition + 1, strSearched, strSought)
    > Loop
    > LastInStr = intLastPosition
    >
    > End Function
    >
    > Can I use this function in ASP, and Where do I put it.
    > Or is there something else I can do?
    >
    > Regards
    > Marco
    > The NEtherlands

    Paul Guest

  5. #4

    Default Re: Get last part of a string

    Hi Paul,

    Tried what you told me but I only get a number returned.
    Don't know how to get the last part of the string extracted.

    txtImagePathAndFile.Value = "Images/Weapon/" &
    UBound(Split(Recordset.Fields("txtImagePathAndFile "),"\"))

    Returns: Images/Weapon/5

    What I want is Images/Weapon/somepicture.jpg
    Split and Ubound find the number of "/" in the string but i want the
    text that is behind that last "/"

    Regards
    MArco


    "Paul" <paul@nospam.com> wrote in message news:<e7mYDPazDHA.1364@TK2MSFTNGP10.phx.gbl>...
    > use split() and take the last entry
    >
    > e.g.
    >
    > urlSplit = split("D:\Marco\ASIC2000\Images\Weapon\leo1schrägm it.jpg","\")
    > msgbox urlSplit(UBound(urlSplit))
    >
    > regards
    > paul
    >
    > "Krechting" <m.krechting@chello.nl> wrote in message
    > news:a71c776d.0312281546.1d025920@posting.google.c om...
    > > Hi All,
    > >
    > > I'm trying to get the last part of a string.
    > > In VB there's a function called lastinstr() who does the work.
    > > But it doesn't seem to work in ASP.
    > >
    > > The string I have is for example:
    > > D:\Marco\ASIC2000\Images\Weapon\leo1schrägmit.jpg
    > > Now I want to get rit of everything before the last "\" so that
    > > leaves: leo1schrägmit.jpg
    > >
    > > One of the problems I have is that I never know what the string
    > > looks like so I always looked for the last instance of "\" and cut
    > > eveything left of it.
    > >
    > > In VB this code did it:
    > >
    > > Function LastInStr(strSearched As String, strSought As String) As Integer
    > >
    > > Dim intCurrVal As Integer
    > > Dim intLastPosition As Integer
    > >
    > > intCurrVal = InStr(strSearched, strSought)
    > > Do Until intCurrVal = 0
    > > intLastPosition = intCurrVal
    > > intCurrVal = InStr(intLastPosition + 1, strSearched, strSought)
    > > Loop
    > > LastInStr = intLastPosition
    > >
    > > End Function
    > >
    > > Can I use this function in ASP, and Where do I put it.
    > > Or is there something else I can do?
    > >
    > > Regards
    > > Marco
    > > The NEtherlands
    Krechting Guest

  6. #5

    Default Re: Get last part of a string

    "Krechting" <m.krechting@chello.nl> wrote in message
    news:a71c776d.0312281546.1d025920@posting.google.c om...
    > Hi All,
    >
    > I'm trying to get the last part of a string.
    > In VB there's a function called lastinstr() who does the work.
    > But it doesn't seem to work in ASP.
    >
    > The string I have is for example:
    > D:\Marco\ASIC2000\Images\Weapon\leo1schrägmit.jpg
    > Now I want to get rit of everything before the last "\" so that
    > leaves: leo1schrägmit.jpg
    >
    > One of the problems I have is that I never know what the string
    > looks like so I always looked for the last instance of "\" and cut
    > eveything left of it.
    >
    > In VB this code did it:
    >
    > Function LastInStr(strSearched As String, strSought As String) As Integer
    >
    > Dim intCurrVal As Integer
    > Dim intLastPosition As Integer
    >
    > intCurrVal = InStr(strSearched, strSought)
    > Do Until intCurrVal = 0
    > intLastPosition = intCurrVal
    > intCurrVal = InStr(intLastPosition + 1, strSearched, strSought)
    > Loop
    > LastInStr = intLastPosition
    >
    > End Function
    >
    > Can I use this function in ASP, and Where do I put it.
    > Or is there something else I can do?
    >
    > Regards
    > Marco
    > The NEtherlands
    Are you familiar with "InStrRev"?

    Description

    Returns the position of an occurrence of one string within another, from the
    end of string.

    Syntax

    InStrRev(string1, string2[, start[, compare]])


    Here's how I use it to get the path to the current script:

    Dim strSFN
    strSFN = WScript.ScriptFullName
    strSFN = Left(strSFN,InStrRev(strSFN,"\"))

    (Note that the above snippet applies to WSH not ASP.)


    McKirahan Guest

  7. #6

    Default Re: Get last part of a string

    Hi Marco

    try this:

    urlSplit = Split(Recordset.Fields("txtImagePathAndFile"),"\")
    txtImagePathAndFile.Value = "Images/Weapon/" & urlSplit(UBound(urlSplit))

    to explain:
    the Split function returns an array of the components of the source string -
    to access the last array member you need to get the index of the last member
    using UBound and then access it by passsing this back into the array

    in my earlier example, the path
    "D:\Marco\ASIC2000\Images\Weapon\leo1schrägmit.jpg " gets passed into an
    array as

    urlSplit(0) --> D:
    urlSplit(1) --> Marco
    urlSplit(2) --> ASIC2000
    urlSplit(3) --> Images
    urlSplit(4) --> Weapon
    urlSplit(5) --> leo1schrägmit.jpg

    UBound(urlSplit) then returns the upper value of the index (in this case 5)

    the value can then be accessed as urlSplit(UBound(urlSplit)) which is the
    same as urlSplit(5) which gives you leo1schrägmit.jpg

    as also posted, you could use InStrRev (probably more efficient as you don't
    have to deal with arrays)

    strPath = "D:\Marco\ASIC2000\Images\Weapon\leo1schrägmit.jpg "
    WScript.Echo Right(strPath,Len(strPath)-InStrRev(strPath,"\"))

    regards
    paul

    "Krechting" <m.krechting@chello.nl> wrote in message
    news:a71c776d.0312290302.5bf733f3@posting.google.c om...
    > Hi Paul,
    >
    > Tried what you told me but I only get a number returned.
    > Don't know how to get the last part of the string extracted.
    >
    > txtImagePathAndFile.Value = "Images/Weapon/" &
    > UBound(Split(Recordset.Fields("txtImagePathAndFile "),"\"))
    >
    > Returns: Images/Weapon/5
    >
    > What I want is Images/Weapon/somepicture.jpg
    > Split and Ubound find the number of "/" in the string but i want the
    > text that is behind that last "/"
    >
    > Regards
    > MArco
    >
    >
    > "Paul" <paul@nospam.com> wrote in message
    news:<e7mYDPazDHA.1364@TK2MSFTNGP10.phx.gbl>...
    > > use split() and take the last entry
    > >
    > > e.g.
    > >
    > > urlSplit =
    split("D:\Marco\ASIC2000\Images\Weapon\leo1schrägm it.jpg","\")
    > > msgbox urlSplit(UBound(urlSplit))
    > >
    > > regards
    > > paul
    > >
    > > "Krechting" <m.krechting@chello.nl> wrote in message
    > > news:a71c776d.0312281546.1d025920@posting.google.c om...
    > > > Hi All,
    > > >
    > > > I'm trying to get the last part of a string.
    > > > In VB there's a function called lastinstr() who does the work.
    > > > But it doesn't seem to work in ASP.
    > > >
    > > > The string I have is for example:
    > > > D:\Marco\ASIC2000\Images\Weapon\leo1schrägmit.jpg
    > > > Now I want to get rit of everything before the last "\" so that
    > > > leaves: leo1schrägmit.jpg
    > > >
    > > > One of the problems I have is that I never know what the string
    > > > looks like so I always looked for the last instance of "\" and cut
    > > > eveything left of it.
    > > >
    > > > In VB this code did it:
    > > >
    > > > Function LastInStr(strSearched As String, strSought As String) As
    Integer
    > > >
    > > > Dim intCurrVal As Integer
    > > > Dim intLastPosition As Integer
    > > >
    > > > intCurrVal = InStr(strSearched, strSought)
    > > > Do Until intCurrVal = 0
    > > > intLastPosition = intCurrVal
    > > > intCurrVal = InStr(intLastPosition + 1, strSearched,
    strSought)
    > > > Loop
    > > > LastInStr = intLastPosition
    > > >
    > > > End Function
    > > >
    > > > Can I use this function in ASP, and Where do I put it.
    > > > Or is there something else I can do?
    > > >
    > > > Regards
    > > > Marco
    > > > The NEtherlands

    Paul Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139