Remove last n characters of string after the dot with Autohotkey

回眸只為那壹抹淺笑 提交于 2019-12-13 19:19:47

问题


I am using Autohotkey.

I have a string that looks like this S523.WW.E.SIMA. I want to remove the last few characters of the string after the dot (including the dot itself). So, after the removal, the string will look like S523.WW.E.

This may look like a simple question but I just cannot figure out using the available string functions in Autohotkey. How can this be done using Autohotkey? Thank you very much.


回答1:


Example 1 (last index of)

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

See InStr
See SubStr

Example 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]
}

Example 3 (RegExMatch)

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

Example 4 (RegExReplace)

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


来源:https://stackoverflow.com/questions/24840273/remove-last-n-characters-of-string-after-the-dot-with-autohotkey

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