问题
i am looking for the best way to check for and strip trailing characters in a variable.
For example, if last character is / then i want to remove it.
Ex1: If $var is C:\mypath\ then i want output as only C:\mypath
Ex2: if $var is C:\mypath then output is C:\mypath
回答1:
Function StripBackslash
Exch $0
Push $1
StrCpy $1 $0 "" -1
StrCmp $1 "\" 0 +2
StrCpy $0 $0 -1
Pop $1
Exch $0
FunctionEnd
Section
Push "c:\some\path\"
Call StripBackslash
Pop $0
DetailPrint |$0|
Push "c:\some\path"
Call StripBackslash
Pop $0
DetailPrint |$0|
;Alternative path validation:
Push $InstDir
StrCpy $InstDir "c:\some\path\"
StrCpy $0 $InstDir
Pop $InstDir
DetailPrint |$0|
SectionEnd
来源:https://stackoverflow.com/questions/19683976/best-way-to-remove-trailing-character-from-a-string-for-example-trailing-slash