class StringLib { static init := ("".base.base := StringLib) static __Set := Func("StringLib_Set") Left(count=1) { return, SubStr(this, 1, count) } Right(count=1) { StringRight, out, this, count return, out } TrimL(count="") { if count.is_Str out := count? LTrim(this, count):LTrim(this) else StringTrimLeft, out, this, count return, out } TrimR(count="") { if count.is_Str return, count? RTrim(this, count):RTrim(this) else StringTrimRight, out, this, count return, out } Count(count) { StringReplace, this, this, %count%, , UseErrorLevel return, ErrorLevel } Replace(find, replace="") { StringReplace, out, this, %find%, %replace%, A return, out } GSub(needle, replace="") { return, RegExReplace(this, needle, replace) } Times(times) { VarSetCapacity(out, times) Loop, %times% out .= this return, out } Split(delim="", omit="") { out := object() if (SubStr(delim, 1, 2) = "R)") { this := this.Replace(omit), pos := 0, n:=start:=1 if ((needle:=SubStr(delim, 3))="") return, this.Split(needle) while, pos := RegExMatch(this, needle, match, start) out[n++] := SubStr(this, start, pos-start), start := pos+StrLen(match) out[n] := SubStr(this, start) } else Loop, Parse, this, %delim%, %omit% out[A_Index] := A_LoopField return, out } RemoveLines(lines) { VarSetCapacity(out, n:=StrLen(this)) Loop, Parse, this, `n, `r if A_Index not in %lines% out .= A_LoopField "`n" return, SubStr(out, 1, -1) } KeepLines(lines) { VarSetCapacity(out, n:=StrLen(this)) Loop, Parse, this, `n, `r if A_Index in %lines% out .= A_LoopField "`n" return, SubStr(out, 1, -1) } __Get(key, key2="") { if (key = "is_int") { if this is integer out := true else, out := false } else if (key = "upper") StringUpper, out, this else if (key = "lower") StringLower, out, this else if (key = "capitalize") out := SubStr(this, 1, 1).upper . SubStr(this, 2).lower else if (key = "reverse") or (key = "rev") DllCall("msvcrt\_" (A_IsUnicode? "wcs":"str") "rev", "UInt",&this, "CDecl"), out:=this else if (key = "length") or (key = "len") out := StrLen(this) else if (key = "isEmpty") out := StrLen(this)? False:True else if key.is_int and key2.is_int out := SubStr(this, key, key2) else if key.is_int and key2="" out := SubStr(this, key, 1) else if (key = "toHex") { format := A_FormatInteger SetFormat, IntegerFast, Hex out := "" this+0 "" SetFormat, IntegerFast, %format% } else if (key = "toDec") { format := A_FormatInteger SetFormat, IntegerFast, Dec out := "" this+0 "" SetFormat, IntegerFast, %format% } else if (key = "is_str") { Format := A_FormatInteger SetFormat, IntegerFast, Hex out := SubStr(this:=this, 1, 2)="0x"? False:True SetFormat, IntegerFast, %Format% } return, out } } StringLib_Set(byref this, key, value){ StringReplace, this, this, %key%, %value%, all }To use this, save the above code as a file named StringLib.ahk in your user Lib folder. Then place #Include <StringLib> at the beginning of your script. (requires AHK_L)
Example Uses:
#Include <StringLib> var := "AutoHotkey" MsgBox % var.Length MsgBox % var[5] MsgBox % var.Upper MsgBox % "AutoHotkey".Lower MsgBox % var.Reverse MsgBox % var.Left(4) MsgBox % var.Right(6) MsgBox % var.Replace("utoHotkey","HK") MsgBox % var[5,3]
#Include <StringLib> var = ( line1 line2 line3 line4 ) MsgBox, % var.RemoveLines("1,3") MsgBox, % var.KeepLines("1,3")