使用Autohotkey删除点后的最后n个字符串

时间:2014-07-19 12:14:00

标签: autohotkey

我正在使用Autohotkey。

我有一个类似于S523.WW.E.SIMA的字符串。我想删除点后面的字符串的最后几个字符(包括点本身)。因此,删除后,字符串将显示为S523.WW.E

这可能看起来像一个简单的问题,但我无法弄清楚使用Autohotkey中的可用字符串函数。如何使用Autohotkey完成此操作?非常感谢你。

1 个答案:

答案 0 :(得分:4)

示例1(最后一个索引)

string := "S523.WW.E.SIMA"

LastDotPos := InStr(string,".",0,0)  ; get position of last occurrence of "."
result := SubStr(string,1,LastDotPos-1)  ; get substring from start to last dot

MsgBox %result%  ; display result

InStr
SubStr

示例2(StrSplit)

; Split it into the dot-separated parts,
; then join them again excluding the last part
parts := StrSplit(string, ".")
result := ""
Loop % parts.MaxIndex() - 1
{
        if(StrLen(result)) {
                result .= "."
        }
        result .= parts[A_Index]
}

示例3(RegExMatch)

; Extract everything up until the last dot
RegExMatch(string, "(.*)\.", result)
msgbox % result1

示例4(RegExReplace)

; RegExReplace to remove everything, starting with the last dot
result := RegExReplace(string, "\.[^\.]+$", "")
相关问题