Trimming the left & right sides of a string Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ozzynotwood
Posts: 30
Joined: 16 Nov 2017, 19:19

Trimming the left & right sides of a string

Post by ozzynotwood » 23 Oct 2021, 17:08

Hello!

Been working on this for a while & not getting anywhere. For use in other things, I need to get & modify a WinTitle for use in an emails subject line so the subject line makes sense to the receiver. I need to remove the first 26 characters & last 16 characters of the Win Title. I can remove the first 26 but despite working on this for much longer than I should have. I can't remove the last 16 characters. Could somebody please take a look at the code & see what I need here? The failed code isn't included but it was just the same concept again with negative numbers). Thanks!

Original Wintitle
Cin7 | Edit Sales Order - Stop Slow Traffic - SSLO620-81 - Google Chrome

Desitred outcome of code
Stop Slow Traffic - SSLO620-81

Code: Select all

5::
WinGetTitle, dv_wintitle, A ; ✅ WORKS
dv_wintitle_formatted := SubStr(dv_wintitle, 26) ; ✅ Works, removes the first 26 characters
; ❌  Various attemps of code was here. Tried a nunch of variations of the line above, using negative numbers. Any other research or only shows my results for depricated code.
MsgBox %dv_wintitle_formatted% ; ✅ Works & shows result. When the outcome is reached, I will remove the message box & have the variables content typed into Outlook

User avatar
mikeyww
Posts: 26596
Joined: 09 Sep 2014, 18:38

Re: Trimming the left & right sides of a string

Post by mikeyww » 23 Oct 2021, 18:16

Code: Select all

dv_wintitle = Cin7 | Edit Sales Order - Stop Slow Traffic - SSLO620-81 - Google Chrome
RegExMatch(dv_wintitle, "- (.+) -", trunc)
MsgBox, 64, Truncated #1, % trunc1
MsgBox, 64, Truncated #2, % trunc2 := SubStr(dv_wintitle, START := 27, -1 * TRUNCATE := 16)

User avatar
Xtra
Posts: 2744
Joined: 02 Oct 2015, 12:15

Re: Trimming the left & right sides of a string

Post by Xtra » 23 Oct 2021, 18:59

Code: Select all

dv_wintitle := "Cin7 | Edit Sales Order - Stop Slow Traffic - SSLO620-81 - Google Chrome"
dv_wintitle_formatted := SubStr(dv_wintitle, 27, StrLen(dv_wintitle) - (26+16))
MsgBox % dv_wintitle_formatted

sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Trimming the left & right sides of a string  Topic is solved

Post by sofista » 23 Oct 2021, 19:15

Code: Select all

winTitle := "Cin7 | Edit Sales Order - Stop Slow Traffic - SSLO620-81 - Google Chrome"

Parts := StrSplit(winTitle, " - ")
MsgBox, % Format("{2} - {3}", Parts*)

;Stop Slow Traffic - SSLO620-81

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Trimming the left & right sides of a string

Post by flyingDman » 23 Oct 2021, 20:50

or:

Code: Select all

var = Cin7 | Edit Sales Order - Stop Slow Traffic - SSLO620-81 - Google Chrome
del := " - ", len := strlen(del)
msgbox % substr(var,x := instr(var,del,,1,1) + len, instr(var,del,,0,1) - x)
14.3 & 1.3.7

ozzynotwood
Posts: 30
Joined: 16 Nov 2017, 19:19

Re: Trimming the left & right sides of a string

Post by ozzynotwood » 24 Oct 2021, 02:17

sofista wrote:
23 Oct 2021, 19:15

Code: Select all

winTitle := "Cin7 | Edit Sales Order - Stop Slow Traffic - SSLO620-81 - Google Chrome"

Parts := StrSplit(winTitle, " - ")
MsgBox, % Format("{2} - {3}", Parts*)

;Stop Slow Traffic - SSLO620-81
Thankyou, I went with this version as it was the smallest. It worked & I'll go to the docs to find out what all this means. Thanks again, this is now part of a much larger code block & its the key part that makes my task much quicker!

sofista
Posts: 645
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: Trimming the left & right sides of a string

Post by sofista » 24 Oct 2021, 10:17

You're welcome, glad it worked for you :thumbup:

In the docs, look for variadic functions.

garry
Posts: 3740
Joined: 22 Dec 2013, 12:50

Re: Trimming the left & right sides of a string

Post by garry » 24 Oct 2021, 11:43

thank you for the examples , also :

Code: Select all

;-                                     Stop Slow Traffic - SSLO620-81
winTitle := "Cin7 | Edit Sales Order - Stop Slow Traffic - SSLO620-81 - Google Chrome"
;-------------------------
c:=StrSplit(winTitle, " - ")
msgbox, % c[2] . " - " . c[3]
the same :

Code: Select all

;-                                     Stop Slow Traffic - SSLO620-81
winTitle := "Cin7 | Edit Sales Order - Stop Slow Traffic - SSLO620-81 - Google Chrome"
;-------------------------
dlm:=" - "                     ;- the delimiter
c:=StrSplit(winTitle,dlm)
aa:=c[2] . dlm . c[3]
msgbox, %aa%
Last edited by garry on 25 Oct 2021, 02:59, edited 1 time in total.

User avatar
flyingDman
Posts: 2791
Joined: 29 Sep 2013, 19:01

Re: Trimming the left & right sides of a string

Post by flyingDman » 24 Oct 2021, 12:34

@ozzynotwood note, your selected solution only works if there is " - " within the text to be captured.
14.3 & 1.3.7

ozzynotwood
Posts: 30
Joined: 16 Nov 2017, 19:19

Re: Trimming the left & right sides of a string

Post by ozzynotwood » 25 Oct 2021, 01:58

flyingDman wrote:
24 Oct 2021, 12:34
@ozzynotwood note, your selected solution only works if there is " - " within the text to be captured.
I will keep this in mind as I learn this, thanks for the tip!

Post Reply

Return to “Ask for Help (v1)”