Page 1 of 1
Divide String by number of characters
Posted: 26 May 2019, 23:43
by DataLife
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%
Re: Divide String by number of characters Topic is solved
Posted: 27 May 2019, 00:53
by Rohwedder
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,%
Re: Divide String by number of characters
Posted: 27 May 2019, 08:38
by DataLife
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)*?,?)
Re: Divide String by number of characters
Posted: 27 May 2019, 08:48
by jeeswg
Using RegEx. Cheers.
Code: Select all
;split text into blocks of 10 characters, CRLF-separated
vText := "FFD8FFE000104A46494600010101006000600000FFDB0043000201010201010202020202020202030503"
MsgBox, % RegExReplace(vText, ".{10}(?!$)", "$0`r`n")
Re: Divide String by number of characters
Posted: 27 May 2019, 09:51
by Rohwedder
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.
Re: Divide String by number of characters
Posted: 27 May 2019, 14:02
by aifritz
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")
Re: Divide String by number of characters
Posted: 27 May 2019, 14:32
by jeeswg
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
Re: Divide String by number of characters
Posted: 27 May 2019, 14:40
by aifritz
Many thanks
![Smile :)](./images/smilies/icon_e_smile.gif)
Re: Divide String by number of characters
Posted: 28 May 2019, 00:11
by DataLife
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.
Re: Divide String by number of characters
Posted: 28 May 2019, 04:35
by gregster
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](./download/file.php?id=7336)
- non-printable unicode characters.png (10.66 KiB) Viewed 3161 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
Re: Divide String by number of characters
Posted: 28 May 2019, 09:53
by garry
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
Re: Divide String by number of characters
Posted: 28 May 2019, 12:37
by sinkfaze
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