[VB] extract string after the last \
sophie26
-
alamata -
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,
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
-
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 ... -
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+. -
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 -