Delete the first digit Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
jonyjohny
Posts: 14
Joined: 05 Mar 2018, 06:06

Delete the first digit

08 Mar 2018, 03:45

Hi :), I have a small problem

In the file that creates for me the autohotkey (.txt file) in the 1st line I have 11 digit code, but unfortunately some times it starts with 0 (read from the left). Do you know what formulas to use to delete 0 if it is the first digit on the left?
I have to delete it because it hinders the further execution of the script. And I can not use the function to delete all zeros, because if it is in the middle of a number it must be there
garry
Posts: 1563
Joined: 22 Dec 2013, 12:50

Re: Delete the first digit

08 Mar 2018, 04:37

found this

Code: Select all

x:=012345608901
aa:=StrTrim(x, "0_\0")
msgbox,%aa%

StrTrim(String, TrimChars)
{
    if !(DllCall("shlwapi.dll\StrTrim", "Ptr", &String, "Ptr", &TrimChars))
        return FALSE
    return StrGet(&String)
}

BoBo
Posts: 2515
Joined: 13 May 2014, 17:15

Re: Delete the first digit

08 Mar 2018, 04:44

Code: Select all

	line := 0123045678900
	MsgBox % line := InStr(SubStr(line,1,1),0) ? StrReplace(line,0,"",,1) : line
	Return
Tested. RegExFree ;)
garry
Posts: 1563
Joined: 22 Dec 2013, 12:50

Re: Delete the first digit

08 Mar 2018, 07:44

thank you BoBo
I'm interesting for 3th example : shoul'd remove leading zero's but not the last zero's

Code: Select all

;---------------------------------------------------------
;- example-1 :  remove first character
k = 0123456089010
stringtrimleft,knew,k,1
msgbox, 262208,1st-Example ,%knew%

;---------------------------------------------------------
;- example-2  :  doesn't remove leading zeros more then one
x:="0123456089010"
y:="001234560890100"
z:="00012345608901000"

x1:= InStr(SubStr(x,1,1),0) ? StrReplace(x,0,"",,1) : x
y1:= InStr(SubStr(y,1,1),0) ? StrReplace(y,0,"",,1) : y
z1:= InStr(SubStr(z,1,1),0) ? StrReplace(z,0,"",,1) : z

msgbox, 262208,2nd-Example ,x1=%x1%`ny1=%y1%`nz1=%z1%

;--------------------------------------------------------
;-- example-3 : remove leading zeros , but remove also last zero's
x2:=StrTrim(x, "0_\0")
y2:=StrTrim(y, "0_\0")
z2:=StrTrim(z, "0_\0")
msgbox, 262208,3th-Example ,x2=%x2%`ny2=%y2%`nz2=%z2%

StrTrim(String, TrimChars)
{
    if !(DllCall("shlwapi.dll\StrTrim", "Ptr", &String, "Ptr", &TrimChars))
        return FALSE
    return StrGet(&String)
}
;========================================================
Odlanir
Posts: 274
Joined: 20 Oct 2016, 08:20

Re: Delete the first digit

08 Mar 2018, 09:20

If I'm not wrong, it seems that the specified Dll can't distinguish between trailing and leading chars.
https://msdn.microsoft.com/it-it/librar ... s.85).aspx
StrTrim function
Removes specified leading and trailing characters from a string.
____________________________________________________________________________
Windows 7 Home Premium 64 bit - Autohotkey v1.1.24.02 64-bit Unicode
garry
Posts: 1563
Joined: 22 Dec 2013, 12:50

Re: Delete the first digit

08 Mar 2018, 11:58

Odlanir , thank you
User avatar
SpeedMaster
Posts: 235
Joined: 12 Nov 2016, 16:09

Re: Delete the first digit

08 Mar 2018, 12:44

Odlanir wrote:If I'm not wrong, it seems that the specified Dll can't distinguish between trailing and leading chars.
https://msdn.microsoft.com/it-it/librar ... s.85).aspx
StrTrim function
Removes specified leading and trailing characters from a string.
Good stuff 8-)
To remove only trailing characters , we could append first a character and remove it after calling StrTrim()

Code: Select all

z:="00012345608901000"
z2:=SubStr(StrTrim(z:="£" z, "0"),2)

msgbox, % z2

return

StrTrim(String, TrimChars)
{
    if !(DllCall("shlwapi.dll\StrTrim", "Ptr", &String, "Ptr", &TrimChars))
        return FALSE
    return StrGet(&String)
}

jonyjohny
Posts: 14
Joined: 05 Mar 2018, 06:06

Re: Delete the first digit

08 Mar 2018, 13:07

Code: Select all

	line := 0123045678900
	MsgBox % line := InStr(SubStr(line,1,1),0) ? StrReplace(line,0,"",,1) : line
	Return
this code to be very help :)
Is it possible to modify it to delete, for example, only 11 digit? I tried to change the values in it, but if I write anywhere 11, It removes all the same items to 11th place, but I want that It delete only 11 items, which also is 0
User avatar
jeeswg
Posts: 5413
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Delete the first digit  Topic is solved

08 Mar 2018, 19:46

Some ideas, including for your last post, which I believe to mean: 'delete the 11th character if it is a 0'.

Code: Select all

q:: ;trim 0 examples
vNum := "000123"
MsgBox, % LTrim(vNum, 0) ;remove leading zeros

vNum := "000123"
MsgBox, % RegExReplace(vNum, "^0") ;if starts with 0, remove it

vNum := "000123"
MsgBox, % RegExReplace(vNum, "^00") ;if starts with 00, remove it

vNum := "000123"
MsgBox, % RegExReplace(vNum, "^000") ;if starts with 000, remove it

vNum := "01230123"
MsgBox, % RegExReplace(vNum, "^.{4}\K0") ;delete 5th character if it is a 0
;^ string must start with needle
;.{4} 4 of any character
;\K do not replace the text before the \K
return
garry
Posts: 1563
Joined: 22 Dec 2013, 12:50

Re: Delete the first digit

09 Mar 2018, 04:04

@jeeswg , thank you for the examples ( at the moment I was specially interesting for the first example ( remove all leading zeros ) )
User avatar
jeeswg
Posts: 5413
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Delete the first digit

09 Mar 2018, 08:09

- Cheers garry np. Yeah, I was surprised nobody else mentioned LTrim, when new functions are added people don't always find out straightaway.
- I thought of one or two further examples that might be useful.

Code: Select all

q:: ;remove 1 or more consecutive 0s

;a+ 1 or more a's
;[a]+ 1 or more a's
;[aeiou]+ 1 or more consecutive vowels
;[^a]+ 1 or more consecutive characters that aren't a
;[^aeiou]+ 1 or more consecutive characters that aren't vowels

vNum := "000123000123000123"
MsgBox, % RegExReplace(vNum, "^0+") ;remove leading zeros
MsgBox, % RegExReplace(vNum, "^.{6}\K0+") ;remove 7th character if it's 0 (and any subsequent adjacent zeros)
MsgBox, % RegExReplace(vNum, "^.{12}\K0+") ;remove 13th character if it's 0 (and any subsequent adjacent zeros)

MsgBox, % RegExReplace(vNum, "^.\K0+") ;remove 2nd character if it's 0 (and any subsequent adjacent zeros)
MsgBox, % RegExReplace(vNum, "^.{7}\K0+") ;remove 8th character if it's 0 (and any subsequent adjacent zeros)
MsgBox, % RegExReplace(vNum, "^.{13}\K0+") ;remove 14th character if it's 0 (and any subsequent adjacent zeros)
return
garry
Posts: 1563
Joined: 22 Dec 2013, 12:50

Re: Delete the first digit

10 Mar 2018, 03:00

@jeeswg , thank you again for the good examples

Return to “Ask For Help”

Who is online

Users browsing this forum: 8bitgunny, blad4, gazmoz17, Gigadroid, Sabestian Caine and 43 guests