且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-02-04 23:22:48

示例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

See InStr
See SubStr

; 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, "\.[^\.]+$", "")