So RegEx and strings are the parts I struggle the most with.
Say String := "This is the original string to clip example."
I want to look for the first occurrence of "to" and clip the string to have the "to" and everything after removed. So the result would be Trimmed_string := "This is the original string "
How do I do that?
Clipping a string?
Re: Clipping a string?
Code: Select all
#Requires AutoHotkey v1.1.33
str := "This is the original string to clip to example."
MsgBox % clip(str, "to")
clip(str, fromWord) {
Return (f := InStr(str, fromWord)) ? SubStr(str, 1, f - 1) : str
}
RegEx could be
Code: Select all
Return RegExReplace(str, fromWord ".*")
Last edited by mikeyww on 08 Jun 2023, 14:24, edited 3 times in total.
Re: Clipping a string?
Hallo,
try:
try:
Code: Select all
String := "This is the original string to clip example."
IF To := InStr(String, "to")
String := SubStr(String, 1, To-1)
MsgBox,% "String:`n" String
Re: Clipping a string?
Thanx guys. This has been EXTREMELY helpful.
Re: Clipping a string?
Another similar solution:
Code: Select all
String := "This is the original string to clip example."
Delimiter := " to "
TrimmedString := StrSplit(String, Delimiter)
TrimmedString := TrimmedString[1]
MsgBox, TrimmedString is: %TrimmedString%
ste(phen|ve) kunkel
Re: Clipping a string?
I guess there is more than one way to skin a cat....interesting!kunkel321 wrote: ↑10 Jun 2023, 10:55Another similar solution:Code: Select all
String := "This is the original string to clip example." Delimiter := " to " TrimmedString := StrSplit(String, Delimiter) TrimmedString := TrimmedString[1] MsgBox, TrimmedString is: %TrimmedString%