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

use*_*486 1 autohotkey

我正在使用自动热键。

我有一个看起来像这样的字符串S523.WW.E.SIMA。我想删除点之后的字符串的最后几个字符(包括点本身)。因此,删除后,字符串将如下所示S523.WW.E

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

Joe*_* DF 5

示例 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
Run Code Online (Sandbox Code Playgroud)

请参阅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]
}
Run Code Online (Sandbox Code Playgroud)

示例 3(RegExMatch)

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

示例 4(RegExReplace)

; RegExReplace to remove everything, starting with the last dot
result := RegExReplace(string, "\.[^\.]+$", "")
Run Code Online (Sandbox Code Playgroud)

  • 我收集了一些替代方案 [here](http://pastebin.com/VBWCvqYZ)。也许,您想将它们添加到您的答案中。 (2认同)