Divide String by number of characters Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
DataLife
Posts: 321
Joined: 29 Sep 2013, 19:52

Divide String by number of characters

26 May 2019, 23:43

I have come up with the code below to divide a string by X number. I need to see if someone can do this more elegantly. Possibly with regex.
For instance if the string was 84 characters in length and I wanted to divide it by 10. I would end of with this...

String = FFD8FFE000104A46494600010101006000600000FFDB0043000201010201010202020202020202030503
Result =
FFD8FFE000
104A464946
0001010100
6000600000
FFDB004300
0201010201
0102020202
0202020203
0503

Each line of characters is a variable.

This code works, but is there a better way?

Code: Select all

SplitStringVarByThisNumber = 10
Var := "FFD8FFE000104A46494600010101006000600000FFDB0043000201010201010202020202020202030503"
Len := StrLen(Var)
Sections := (Len / SplitStringVarByThisNumber)
Sections := ceil(Sections)

loop %Sections%
{
 if A_index = 1
  GetCharsStartingAt := (a_index)
 if a_index = 2
  GetCharsStartingAt := ((SplitStringVarByThisNumber) + 1)
 if a_index > 2
  GetCharsStartingAt := (((a_index - 1) * SplitStringVarByThisNumber) + 1)
 NewStr%a_index% := SubStr(Var, GetCharsStartingAt , SplitStringVarByThisNumber)
}

Loop %Sections%
 DisplayVar := DisplayVar "`n" . NewStr%a_index%
MsgBox %DisplayVar%
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
Rohwedder
Posts: 2049
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Divide String by number of characters  Topic is solved

27 May 2019, 00:53

Hallo,
try:

Code: Select all

SplitStringVarByThisNumber = 10
Var := "FFD8FFE000104A46494600010101006000600000FFDB0043000201010201010202020202020202030503"
While, NewStr%A_Index% := SubStr(Var,1+(A_Index-1)*SplitStringVarByThisNumber, SplitStringVarByThisNumber)
	DisplayVar .= NewStr%A_Index% "`n"
MsgBox %DisplayVar%
or (just for fun!!!):

Code: Select all

​.=SplitStringVarByThisNumber := 10
Var := "FFD8FFE000104A46494600010101006000600000FFDB0043000201010201010202020202020202030503"
While, NewStr%A_Index% := SubStr(Var,1+(A_Index-1)*​,​)
	​​.=NewStr%A_Index% "`n"
MsgBox,% ​​
User avatar
DataLife
Posts: 321
Joined: 29 Sep 2013, 19:52

Re: Divide String by number of characters

27 May 2019, 08:38

Rohwedder wrote:
27 May 2019, 00:53
Hallo,
try:

Code: Select all

SplitStringVarByThisNumber = 10
Var := "FFD8FFE000104A46494600010101006000600000FFDB0043000201010201010202020202020202030503"
While, NewStr%A_Index% := SubStr(Var,1+(A_Index-1)*SplitStringVarByThisNumber, SplitStringVarByThisNumber)
	DisplayVar .= NewStr%A_Index% "`n"
MsgBox %DisplayVar%
or (just for fun!!!):

Code: Select all

​.=SplitStringVarByThisNumber := 10
Var := "FFD8FFE000104A46494600010101006000600000FFDB0043000201010201010202020202020202030503"
While, NewStr%A_Index% := SubStr(Var,1+(A_Index-1)*​,​)
	​​.=NewStr%A_Index% "`n"
MsgBox,% ​​
The first one works great.
The second one does not even display the message box. It just ends after .36 seconds

The copy and paste to my editor put a ? before .=SplitStringVarByThisNumber := 10 and also here

Code: Select all

While, NewStr%A_Index% := SubStr(Var,1+(A_Index-1)*?,?)
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
User avatar
jeeswg
Posts: 6538
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Divide String by number of characters

27 May 2019, 08:48

Using RegEx. Cheers.

Code: Select all

;split text into blocks of 10 characters, CRLF-separated
vText := "FFD8FFE000104A46494600010101006000600000FFDB0043000201010201010202020202020202030503"
MsgBox, % RegExReplace(vText, ".{10}(?!$)", "$0`r`n")
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Rohwedder
Posts: 2049
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Divide String by number of characters

27 May 2019, 09:51

Hallo DataLife,
the second one contains invisible unicode.
Use Select all of the codebox, copy and paste.
Tested with Notepad++.
But it's just a joke anyway.
aifritz
Posts: 130
Joined: 29 Jul 2018, 11:30
Location: Germany

Re: Divide String by number of characters

27 May 2019, 14:02

Hi jeeswg,
very cool code. Could you explain me why you use "(?!$)"?

For me it seems to work also with:

Code: Select all

;split text into blocks of 10 characters, CRLF-separated
vText := "FFD8FFE000104A46494600010101006000600000FFDB0043000201010201010202020202020202030503"
MsgBox, % RegExReplace(vText, ".{10}", "$0`r`n")
User avatar
jeeswg
Posts: 6538
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Divide String by number of characters

27 May 2019, 14:32

Hello aifritz, here's an explanation:

Code: Select all

;with '(?!$)':
MsgBox, % RegExReplace("abcabca", ".{3}(?!$)", "$0_") ;abc_abc_a
MsgBox, % RegExReplace("abcabcab", ".{3}(?!$)", "$0_") ;abc_abc_ab
MsgBox, % RegExReplace("abcabcabc", ".{3}(?!$)", "$0_") ;abc_abc_abc ;no trailing underscore

;without '(?!$)':
MsgBox, % RegExReplace("abcabca", ".{3}", "$0_") ;abc_abc_a
MsgBox, % RegExReplace("abcabcab", ".{3}", "$0_") ;abc_abc_ab
MsgBox, % RegExReplace("abcabcabc", ".{3}", "$0_") ;abc_abc_abc_ ;trailing underscore
Look-ahead assertions are mentioned here:
Regular Expressions (RegEx) - Quick Reference | AutoHotkey
https://autohotkey.com/docs/misc/RegEx-QuickRef.htm
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
aifritz
Posts: 130
Joined: 29 Jul 2018, 11:30
Location: Germany

Re: Divide String by number of characters

27 May 2019, 14:40

Many thanks :)
User avatar
DataLife
Posts: 321
Joined: 29 Sep 2013, 19:52

Re: Divide String by number of characters

28 May 2019, 00:11

Rohwedder wrote:
27 May 2019, 09:51
Hallo DataLife,
the second one contains invisible unicode.
Use Select all of the codebox, copy and paste.
Tested with Notepad++.
But it's just a joke anyway.
Thanks Rohwedder your first example is exactly what I needed.
I never heard of "invisible Unicode". I use Scite4AutoHotkey and the invisible unicode character did not copy and paste even when I used select all.
Check out my scripts. (MyIpChanger) (ClipBoard Manager) (SavePictureAs)
All my scripts are tested on Windows 10, AutoHotkey 32 bit Ansi unless otherwise stated.
gregster
Posts: 2655
Joined: 30 Sep 2013, 06:48

Re: Divide String by number of characters

28 May 2019, 04:35

DataLife wrote:
28 May 2019, 00:11
I use Scite4AutoHotkey and the invisible unicode character did not copy and paste even when I used select all.
Set File>Encoding to UTF-8 with BOM before you paste it into Scite.

Btw, this is what is actually going on:
non-printable unicode characters.png
non-printable unicode characters.png (10.66 KiB) Viewed 404 times
(I used https://www.soscisurvey.de/tools/view-chars.php to visualize it.)

U+200B is the so-called zero-width space character: https://en.wikipedia.org/wiki/Zero-width_space
garry
Posts: 1834
Joined: 22 Dec 2013, 12:50

Re: Divide String by number of characters

28 May 2019, 09:53

another example with modulo

Code: Select all

;- or modulo
Var := "FFD8FFE000104A46494600010101006000600000FFDB0043000201010201010202020202020202030503"
loop,parse,var
{
	if Mod(a_index,10)=0
       e .= a_loopfield . "`r`n"
    else
       e .= a_loopfield
}
msgbox,%e%
return
User avatar
sinkfaze
Posts: 609
Joined: 01 Oct 2013, 08:01

Re: Divide String by number of characters

28 May 2019, 12:37

Another way to do it, just as an interesting exercise:

Code: Select all

var=FFD8FFE000104A46494600010101006000600000FFDB0043000201010201010202020202020202030503
Loop %	(StrLen(var) // 10)
	out .=	SubStr(var,((A_Index-1)*10)+1,10) "`n"
out .=	SubStr(var,-(Mod(StrLen(var),10))+1)
MsgBox %	out

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], ineuw, reluct and 146 guests