[VB] extract string after the last \

sophie26 -  
 alamata -
Hello,
I am looking to extract from any string the content that follows the last \ in my string.
I am using in my code the Instr, Mid functions and the whole family, but I think I'm going around in circles.
Thank you to anyone who puts me back on the right path ^^

sophie,
Configuration: Windows XP Internet Explorer 7.0

4 answers

  1. Steff
     
    Hello Sophie,

    The variable "chaine" contains the text from which we want to retrieve the end.
    2 ways:

    - "Talkative" way:

    pos=InStrRev(chaine,"\")
    result=(Mid(chaine,pos))

    - Less "Talkative" way:
    result=(Mid(chaine,InStrRev(chaine,"\")))

    To check if we need to add +1 to pos ...
    2
  2. amigo
     
    Hello,

    The function extraire_dernier below returns the last element of a string where each field is separated by "\\" as in a file path.
     Sub test() Dim str As String, resultStr As String str = "C:\Documents and Settings\Default User\Local Settings\Temporary Internet Files\Content.IE5" resultStr = extraire_dernier(str) MsgBox resultStr End Sub Function extraire_dernier(str As String) As String Dim pos As Integer, lastPos As Integer lastPos = 0: pos = 0 Do pos = InStr(pos + 1, str, "\") If pos > 0 Then lastPos = pos Loop While pos > 0 extraire_dernier = "" If lastPos > 0 Then extraire_dernier = Right(str, Len(str) - lastPos) End Function 

    A+.
    1
    1. alamata
       
      Hello everyone,....

      complete beginner in vb

      I would like to know how to call this function...

      because this piece of code perfectly meets my needs,...

      sorry to ask this kind of question but you have to start somewhere

      looking forward to a solution
      0
  3. zavenger Posted messages 817 Status Member 161
     
    Hello,

    What language are you exactly using?

    in vbs it would look something like this right(toto,len(toto)-InStrRev(toto,"\")

    I hope this helps you
    0
  4. sophie26
     
    It's for vb6, I'm looking at everything directly, thank you.
    0