Best way to remove trailing character from a string (for example, trailing slashes)

随声附和 提交于 2019-12-24 09:53:54

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!