从字符串中删除尾随字符的最佳方法(例如,尾随斜杠) [英] Best way to remove trailing character from a string (for example, trailing slashes)

查看:57
本文介绍了从字符串中删除尾随字符的最佳方法(例如,尾随斜杠)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找检查和删除变量中尾随字符的最佳方法.

i am looking for the best way to check for and strip trailing characters in a variable.

例如,如果最后一个字符是/那么我想删除它.

Ex1:如果 $var 是 C:\mypath\ 那么我只希望输出为 C:\mypath

Ex1: If $var is C:\mypath\ then i want output as only C:\mypath

Ex2:如果 $var 是 C:\mypath 那么输出是 C:\mypath

Ex2: if $var is C:\mypath then output is C:\mypath

推荐答案

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

这篇关于从字符串中删除尾随字符的最佳方法(例如,尾随斜杠)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆