Jump to content

how to split a string after 4 characters ?


LIMITER
 Share

Recommended Posts

Here is my feeble attempt

#include<string.au3>
#include <File.au3>
_FileCreate("c:\test.txt")
$in = "12341234123412341234"
For $i = 4 To 19 Step 5
    $in = _StringInsert($in, "-", $i)
Next
_FileWriteToLine("c:\test.txt", 1, $in, 1)
Edited by BigDod


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

Yet another, etc... assemble while generating the random:

$sTxt = ""
For $n = 1 To 5
    $sTxt &= Random(0, 9999, 1)
    If $n < 5 Then $sTxt &= "-"
Next
MsgBox(64, "Result", "$sTxt = " & $sTxt)oÝ÷ Øw±½êìÚºÚ"µÍÌÍÜÕH   ][ÝÉ][ÝÂÜ  ÌÍÛHHÈ
BIÌÍÜÕ  [ÏH^
[ÛJ
MHHKJK
BRY ÌÍÛ  È
H[  ÌÍÜÕ    [ÏH    ][ÝËI][ÝÂ^ÙÐÞ
    ][ÝÔÝ[   ][ÝË  ][ÝÉÌÍÜÕH ][ÝÈ  [È ÌÍÜÕ
oÝ÷ Ø    i©îêâr÷«²*'jëh×6$avChars = StringSplit("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", "")
$sTxt = ""
For $i = 1 To 5
    For $n = 1 To 4
        $sTxt &= $avChars[Random(1, 36, 1)]
    Next
    If $i < 5 Then $sTxt &= "-"
Next
MsgBox(64, "Result", "$sTxt = " & $sTxt)

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 1 month later...

Ok, I know this is an old(er) topic, so I apologize... BUT- This is all great unless you want to add the "-" in realtime. For example, I am using a GUICtrlCreateInput() and am trying to automatically add the "-" after every x character (in realtime)

For example: The user types 1234, the input boxes throws "-", the user types 5678, Input throws "-", etc, up to the max (which, including the hyphens, is 25 characters).

Here's what I've tried:

CODE
$VirusKeyIn = GUICtrlRead($VirusKey)

For $i = 4 To 19 Step 5

$VirusKeyIn = _StringInsert($VirusKeyIn, "-", $i)

GUICtrlSetData($VirusKey, $VirusKeyIn)

Next

Also tried:

CODE
$VirusKeyIn = GUICtrlRead($VirusKey)

For $i = 4 To 19 Step 5

If StringLen($VirusKeyIn) > $i Then $VirusKeyIn = _StringInsert($VirusKeyIn, "-", $i)

GUICtrlSetData($VirusKey, $VirusKeyIn)

Next

Here's the interesting thing: When refreshed (this Input works for two different keys, so when I switch it to the other key using the ComboBox and then switch back), THEN it throws in the key! Is what I'm attempting to do even possible with AutoIt?

Thank you so much!

Link to comment
Share on other sites

You're in luck. My cookie dough is chilling in the refrigerator before I start baking, and I'm bored:

#include <GuiConstants.au3>

Opt("GuiOnEventMode", 1)

Global $hGUI, $Input1, $sText = ""

$hGUI = GuiCreate("Test", 300, 200)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")
GUICtrlCreateLabel("Enter your key as xxxx-xxxx-xxxx-xxxx", 10, 10, 280, 20)
$Input1 = GUICtrlCreateInput("", 10, 40, 280, 20)
GUICtrlCreateButton("OK", 100, 140, 100, 30)
GUICtrlSetOnEvent(-1, "_OK")
AdlibEnable("_FormatInput", 250)
GUISetState()

While 1
    Sleep(20)
WEnd

Func _FormatInput()
    Local $sRead = ControlGetText($hGUI, "", $Input1)
    If $sRead <> $sText Then
        $sRead = StringStripWS(StringReplace($sRead, "-", ""), 8)
        Local $sWrite = ""
        For $n = 1 To StringLen($sRead) Step 4
            $sWrite &= StringMid($sRead, $n, 4) & "-"
        Next
        $sWrite = StringTrimRight($sWrite, 1)
        $sText = $sWrite
        ControlSetText($hGUI, "", $Input1, $sWrite)
        ControlSend($hGUI, "", $Input1, "{END}")
        If StringLen($sWrite) = 19 Then _OK()
    EndIf
EndFunc

Func _OK()
    ; Get input
    Local $sRead = ControlGetText($hGUI, "", $Input1)
    Local $avRead = StringSplit($sRead, "-")
    
    ; Test for valid format
    Local $fValid = True
    If $avRead[0] = 4 Then 
        For $n = 1 To 4
            If StringLen($avRead[$n]) <> 4 Then 
                $fValid = False
                ExitLoop
            EndIf
        Next
    Else
        $fValid = False
    EndIf
    
    ; Show results
    If $fValid Then 
        MsgBox(64, "Congratulations!", "You entered a valid key format:  " & $sRead)
        Exit
    Else
        MsgBox(16, "Error!", "Your key format is invalid:  " & $sRead & @CRLF & "No soup for you!")
        ControlSetText($hGUI, "", $Input1, "")
    EndIf
EndFunc

Func _Quit()
    Exit
EndFunc

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

If you want to stringsplit by a certain number of characters, use _StringChop

#include <Array.au3>

_ArrayDisplay(_StringChop("1234-1234-1234-1234",5))

Func _StringChop($string, $size)
$count = Ceiling(StringLen($string)/$size)
Dim $array[$count+1], $start = 1
For $i = 1 To $count
    $array[$i] = StringMid($string, $start, $size)
    $start += $size
Next
$array[0] = $count
Return $array
EndFunc
Link to comment
Share on other sites

  • Moderators

If you want to stringsplit by a certain number of characters, use _StringChop

#include <Array.au3>

_ArrayDisplay(_StringChop("1234-1234-1234-1234",5))

Func _StringChop($string, $size)
$count = Ceiling(StringLen($string)/$size)
Dim $array[$count+1], $start = 1
For $i = 1 To $count
    $array[$i] = StringMid($string, $start, $size)
    $start += $size
Next
$array[0] = $count
Return $array
EndFunc
Or

_SomeOtherStringChop :)

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.

Link to comment
Share on other sites

Ok, I know this is an old(er) topic, so I apologize... BUT- This is all great unless you want to add the "-" in realtime. For example, I am using a GUICtrlCreateInput() and am trying to automatically add the "-" after every x character (in realtime)

For example: The user types 1234, the input boxes throws "-", the user types 5678, Input throws "-", etc, up to the max (which, including the hyphens, is 25 characters).

Here's what I've tried:

CODE
$VirusKeyIn = GUICtrlRead($VirusKey)

For $i = 4 To 19 Step 5

$VirusKeyIn = _StringInsert($VirusKeyIn, "-", $i)

GUICtrlSetData($VirusKey, $VirusKeyIn)

Next

Also tried:

CODE
$VirusKeyIn = GUICtrlRead($VirusKey)

For $i = 4 To 19 Step 5

If StringLen($VirusKeyIn) > $i Then $VirusKeyIn = _StringInsert($VirusKeyIn, "-", $i)

GUICtrlSetData($VirusKey, $VirusKeyIn)

Next

Here's the interesting thing: When refreshed (this Input works for two different keys, so when I switch it to the other key using the ComboBox and then switch back), THEN it throws in the key! Is what I'm attempting to do even possible with AutoIt?

Thank you so much!

I would have had a temporary buffer, storing up to 4 chars in it, and when it would hit 4, it would add a - to the Edit control, and empty the buffer again.

also I would perhaps store the "old" value in the Edit control so I could check whenever it has changed(to avoid having to check it all the time, etc.)

Link to comment
Share on other sites

Thank you so much! This had me stumped for days! Also, thanks for the reference to the Soup Nazi, PsaltyDS! Enjoy your cookies!

Glad it helped. The cookies are done now, and they came out great!

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • Moderators

Glad it helped. The cookies are done now, and they came out great!

:)

Hey Salty... could have made that function 1 line :) :
$sString = "1234123412341234"
$sTemp = StringTrimRight(StringRegExpReplace($sString, "\d{4}", "\0-"), 1)
MsgBox(0, 0, $sTemp)

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.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...