Jump to content
Sign in to follow this  
TehWhale

How to cut the string every 100 characters?

Recommended Posts

TehWhale

I have a string about 16000 characters long. I want to split this string every 100 characters, so for example.

1202151501245120

and split this one every 5 characters, it would look like this:

12025 & @CRLF & 15012 & @CRLF & 45120

Thanks for your help!

Share this post


Link to post
Share on other sites
sherkas

Look at the method StringTrimLeft ( "string", count )

Have 2 variables. 1 has your original string, the next is a temp variable.

Cut X amount off and save the "trimmed" part via the function above. Then just loop until the original string is less then X.

Very general, but thats the logic you could use to accomplish it.

eg.

func display( $string, $count )

$temp = ""

While StringLen( $string ) > $count

$temp = stringleft( $count ) ;Our cut off part

$string = stringtrimleft( $string, $count ) ;New string trimmed.

;Display temp somehow or deal with it somehow.

WEnd

endfunc

** The above is a rough function, dont copy and paste it.

Share this post


Link to post
Share on other sites
Xenobiologist

Hi,

not very fast, but should work

#include <Array.au3>

$Str = 1202151501245126
$re = _cutString($Str, 3)
_ArrayDisplay($re)

Func _cutString($string, $number)
    Local $ar[(StringLen($string) / $number)], $i = 0
    Do
        $ar[$i] = StringLeft($string, $number)
        $string = StringTrimLeft($string, $number)
        $i += 1
    Until StringLen($string) < $number
    If StringLen($string) > 0 Then
        ReDim $ar[UBound($ar) + 1]
        $ar[UBound($ar) - 1] = $string
    EndIf
    Return $ar
EndFunc  ;==>_cutString

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
Bowmore

And using StringRegExpReplace method

$Var = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
$Var = StringRegExpReplace($var,"(.{10})","\0" & @CRLF)
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$Var' & @lf & @lf & 'Return:' & @lf & $Var) ;### Debug MSGBOX

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites
SmOke_N

And using StringRegExpReplace method

$Var = "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
$Var = StringRegExpReplace($var,"(.{10})","\0" & @CRLF)
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$Var' & @lf & @lf & 'Return:' & @lf & $Var) ;### Debug MSGBOX
That would be my preferred method, I'm confused on how \0 works there though, I'd have assumed it to be \1.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Bowmore

That would be my preferred method, I'm confused on how \0 works there though, I'd have assumed it to be \1.

The help file specifies \0 as the first captured element. But strangely \0 and \1 give exactly the same result.


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites
SmOke_N

The help file specifies \0 as the first captured element. But strangely \0 and \1 give exactly the same result.

I'm not sure that's how it's worded for \0 (as the first capture). I was always under the impression that the "back reference" referred to the parenthesis used.

If you did: (.{10})(.{10}) and used "\0\1" it should fail, however, if you used "\1\2" it would succeed.

Not desired output:

$Var = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
$Var = StringRegExpReplace($var,"(.{5})(.{5})","\0\1" & @CRLF)
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$Var' & @lf & @lf & 'Return:' & @lf & $Var);### Debug MSGBOX

Desired output:

$Var = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
$Var = StringRegExpReplace($var,"(.{5})(.{5})","\1\2" & @CRLF)
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$Var' & @lf & @lf & 'Return:' & @lf & $Var);### Debug MSGBOX

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
Bowmore

I'm not sure that's how it's worded for \0 (as the first capture). I was always under the impression that the "back reference" referred to the parenthesis used.

If you did: (.{10})(.{10}) and used "\0\1" it should fail, however, if you used "\1\2" it would succeed.

Not desired output:

$Var = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
$Var = StringRegExpReplace($var,"(.{5})(.{5})","\0\1" & @CRLF)
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$Var' & @lf & @lf & 'Return:' & @lf & $Var);### Debug MSGBOX

Desired output:

$Var = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
$Var = StringRegExpReplace($var,"(.{5})(.{5})","\1\2" & @CRLF)
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$Var' & @lf & @lf & 'Return:' & @lf & $Var);### Debug MSGBOX
I've just done a bit of experimenting and it would seem that back reference \0 identifies all the back references. in your examples \0 on its own is the same as using \1\2 In my first post because there was only 1 back reference using \0 or \1 gave the same result.

You are correct in saying that \1 is the correct identifier for the first back reference.

One more new thing that I have learned today.


"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites
SmOke_N

I've just done a bit of experimenting and it would seem that back reference \0 identifies all the back references. in your examples \0 on its own is the same as using \1\2 In my first post because there was only 1 back reference using \0 or \1 gave the same result.

You are correct in saying that \1 is the correct identifier for the first back reference.

One more new thing that I have learned today.

Yep, I was playing too and noticed that ;) ... ditto on the new thing.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
TehWhale

Hmm, it doesn't split it into 100's.

I see how its working, but I see no way in changing it to support 100.

Also, how would it start every line, and end every line with: "

Edited by Alienware

Share this post


Link to post
Share on other sites
SmOke_N

Hmm, it doesn't split it into 100's.

I see how its working, but I see no way in changing it to support 100.

Also, how would it start every line, and end every line with: "

Not quite sure what you mean:
$Var = "1234567890abcdefghijklmnopqrstuvwxyz" & _
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321" & _
    "ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba"
$Var = StringRegExpReplace($var,"(.{100})",'"\1"' & @CRLF)
MsgBox(262144,'Debug line ~' & @ScriptLineNumber,'Selection:' & @lf & '$Var' & @lf & @lf & 'Return:' & @lf & $Var) ;### Debug MSGBOX
Edited by SmOke_N
Fixed word wrap

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Share this post


Link to post
Share on other sites
TehWhale

Basically, I have this binary code for an .exe file, an I want to put it in my script. But, I need to have the string split up, so I can see it, but also have ", and " at end and beggining of every line, so I can just copy/paste it, into my script, so it can be written to a .exe.

Share this post


Link to post
Share on other sites
Bowmore

Basically, I have this binary code for an .exe file, an I want to put it in my script. But, I need to have the string split up, so I can see it, but also have ", and " at end and beggining of every line, so I can just copy/paste it, into my script, so it can be written to a .exe.

This works for me

$Exe = "X:\AutoIt3Data\Scripts\Temp\Temp.exe"
$Size = FileGetSize ( $Exe )
$h = FileOpen ( $Exe, 4 )
$Var = FileRead($h,$Size)
$Var = StringRegExpReplace($var,"(.{1,100})",'"\1"' & @CRLF)
ConsoleWrite($Var)
FileClose($h)

Sample output

.
<snipped to save space>
.
.
"14948D5525A77B823CC88B0B678175E21A4AA0F19C306C46F13C245FA147E59DFD17E67CC3542DD0E522DD2188A24D7D

20CC"
"19314A5F575558D57F5D13811985CDB4374B3B31379569C551D5CC05F7B4D93D4DC2387AFC1EA35E4386FC08A94E7AB5

43AB"
"61BCF928BAA4F27F58DD8CAE75759A2ACCBD3B9658216286447C04E3E86F60508B2046E81AC71A6E2AC0471F2EB2E1BA

1CEF"
"0CAB7C56C6BF06452104F5631E15E7C048430E5306636EB55106E90765A958F3766B5D8E5F18F1115CE7CCCB12401E7C

C75F"
"647826F7534B944AFFD91EE43E3ABEAD83275AA5EEC7041E4401866329303CDE2B13BED9CB80BCF6853B1F029FF06D25

7E99"
"D377BA3D9555336D52C2A22C0099141605F59658E9D08DAA88EF2C3E3A6AC7B681BD179F20FAA23903E7793E86893A85

1F09"
"584FD080269852D88ADEBF85D5F9CDF4CA6E66691141748FED164F4EB46A1C3435403831ADA82AB1600FC3EDAEE674EB

536C"
"35EA36EF766FC2422F50275FD8666BF43FEC9E2D0B390FDB0F4DCDF263001ABDA304B45789A77F1DDC6761ACDA859F54

83DC"
"4D9DFB6140EC94C26F6625317F109CE72BF4196EEA79D864F36A4D44D7336C678D68C6C148E176A2EA37641D19D1A4D1

F393"
"07E55A36CCAD8EAAF67EED5CA667C6C59971CB9B463D6E773883B594B004BAF30B77FA3D8853C4F9CADB37BB738742D3

A816"
"029061F11E4CD3FB530913155A4F495D0752745136437FA80EEAA595B0A25B44E9A6D72F736D6783BC61D4DB75616BDB

6E20"
"4B9A159D383767B2CE1169DA10316013E1BD1D2C40CE43A2C76A99546ADD3998DD41DABAF257E3D518D06AF81EE93988

4BC7"
"D9626AA9287165E650178197BDA444005AFEFAEE79D39222E5CC1A5ECAD30D36E602E9C347848A4CF38518BE3ABA8B4A

5324"
"3B18C1F18B488A0F131B80E1FB9AD310D3435B4197806CA16F7AA3D42312DEF302CB21FF2792767CB73F6312606081B1

2DEB"
"D23C63871EBC5DDA1B52082E47ED5B92F562AC187D5542384155332145413036"

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

Share this post


Link to post
Share on other sites
picaxe

Regexp's don't work for binary data because the PCRE regexp engine used in AutoIt interprets a null or Chr(0) as a string terminator. It's probably best to use one of the non regexp solutions previously suggested.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.