StringTrimRight deprecated, what newer method? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
richolland
Posts: 2
Joined: 29 May 2016, 14:07

StringTrimRight deprecated, what newer method?

25 Jul 2021, 09:11

I try to avoid deprecated methods, so I'm trying to find if there is a newer/better way that replaces "StringTrimRight" that I'm just not understanding?

I want an easy way to trim the last three characters from the right of strings which are of varied lengths, such as: 3043, 75607, 159887? This was very simple with the deprecated StringTrimRight.

Neither SubStr() nor rtrim() seem to do this with variable strings of unknown length.

This gives me what I want, but but is there a "newer" way that's not obsolete?

Code: Select all

InVar = 41085	; 3043, 159887
StringTrimRight, OutVar, InVar, 3
MsgBox,1, MESSAGE TITLE, %OutVar%, 10
--
Thank you very much
RH
gregster
Posts: 8990
Joined: 30 Sep 2013, 06:48

Re: StringTrimRight deprecated, what newer method?  Topic is solved

25 Jul 2021, 09:18

Try SubStr's length parameter (third one; optional)

Code: Select all

InVar = 41085	; 3043, 159887
OutVar := SubStr(InVar, 1, -3)
MsgBox,1, MESSAGE TITLE, %OutVar%, 10
https://www.autohotkey.com/docs/commands/SubStr.htm#Parameters wrote:You can also specify a negative Length to omit that many characters from the end of the returned string
sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: StringTrimRight deprecated, what newer method?

25 Jul 2021, 10:19

Or a regex approach

Code: Select all

InVar = 41085	; 3043, 159887
OutVar := RegExReplace(InVar, ".*\K\d{3}")
MsgBox,1, MESSAGE TITLE, %OutVar%, 10
This pattern only deals with numbers, as example data show, but you could use .*\K.{3} for any other character.
teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: StringTrimRight deprecated, what newer method?

25 Jul 2021, 10:42

sofista wrote: Or a regex approach

Code: Select all

InVar := 41085
MsgBox % RegExReplace(InVar, "...$")
sofista
Posts: 650
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: StringTrimRight deprecated, what newer method?

25 Jul 2021, 19:26

teadrinker wrote:
25 Jul 2021, 10:42
sofista wrote: Or a regex approach

Code: Select all

InVar := 41085
MsgBox % RegExReplace(InVar, "...$")
C'mon @teadrinker you'll make people think that regex is simple and easy to understand :D
richolland
Posts: 2
Joined: 29 May 2016, 14:07

Re: StringTrimRight deprecated, what newer method?

28 Jul 2021, 08:52

These all worked. I clicked "Accept answer" to the first one, though each functions well. And most importantly, they're simple enough for me. ;)
Thank y’all very much.

r.h.

Code: Select all

InVar = 41085	; 3043, 159887
OutVar := SubStr(InVar, 1, -3)
MsgBox,1, MESSAGE TITLE, %OutVar%

Code: Select all

InVar = 41085	; 3043, 159887
OutVar := RegExReplace(InVar, ".*\K\d{3}")
MsgBox,1, MESSAGE TITLE, %OutVar%

Code: Select all

InVar = 41085	; 3043, 159887
MsgBox % RegExReplace(InVar, "...$")
[/code]

--end--

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Billykid, Chunjee, Google [Bot], icyolive, inseption86, joedf, Swiftly9767 and 293 guests