定义了一个 LineDelete 函数,功能是删除指定位置的行或一段行区间。代码包含了删除指定行、删除最后一行以及处理倒数第 N 行的操作。
Var =
(
One
Two
Three
Four
Five
)
LineDelete(Var, 2) ; 删除了第2行。
MsgBox, % LineDelete(Var, 2) ; 返回变量的内容,不包括第2行。
; 删除最后一行。同样地,输入-2将删除倒数第二行,以此类推。
MsgBox, % LineDelete(Var, -1) ; 返回变量的内容,不包括最后一行。
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=46520
LineDelete(Str, Pos, EndPos := "", Opts := "", ByRef Match := ""){
If (Pos = "" || Pos = 0 || Pos * 1 = "")
Return Str
LF := (inStr(Str, "`r`n")) ? ("`r`n")
: (inStr(Str, "`n")) ? ("`n") : ("")
L := StrLen(LF)
Str := LF . StrReplace(Str, LF, LF, NumLines) . LF
NumLines := (SubStr(Str, 0) != LF) ? (++NumLines) : (++NumLines)
Pos := (Pos < 0 && Pos >= -NumLines) ? (Pos + NumLines + 1)
: (Pos < -NumLines || Pos > NumLines) ? ("") : (Pos)
EndPos := (Pos = "" || EndPos * 1 = "" || EndPos = 0
|| EndPos < -NumLines || EndPos > NumLines) ? ("")
: (EndPos < 0) ? (EndPos + NumLines + 1) : (EndPos)
(EndPos != "" && EndPos < Pos)
? (Tmp := Pos, Pos := EndPos, EndPos := Tmp, Tmp := "")
Opts := ((Opts != "B" && Opts != "") || (Pos = EndPos && Opts = "B")
|| (EndPos = "" && Opts = "B")) ? ("") : (Opts)
StartMatch := (Opts = "") ? (InStr(Str, LF,,, Pos) + L)
: (InStr(Str, LF,,, Pos + 1) + L)
EndMatch := (EndPos = "" && Opts = "") ? (InStr(Str, LF,,, Pos + 1))
: (EndPos != "" && Opts != "") ? (InStr(Str, LF,,, EndPos))
: (InStr(Str, LF,,, EndPos + 1))
Match := ((EndPos - Pos = 1 && Opts = "B")
|| Pos = "") ? ("")
: (SubStr(Str, StartMatch, EndMatch - StartMatch))
Str := SubStr(SubStr(Str, 1, StartMatch - 1)
. SubStr(Str, EndMatch + L), L + 1, -L)
Return Str
}
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。

评论(0)