Jump to content

How to Add separators between characters


Recommended Posts

How can I add delimiters between the characters I set, without any loop?

For example I want it to look like this => ABCD-EF12-3456

$SerialStringExample1 = "ABCDEF123456"
$delimiters1 = StringRegExpReplace($SerialStringExample1, '\w{4}', '-')
MsgBox(0,0, $delimiters1)

$SerialStringExample2 = "ABCDEF123456"
$delimiters2 = StringLeft($SerialStringExample2, 4)
MsgBox(0,0, $delimiters2)

 

Link to comment
Share on other sites

  • Moderators

Zero0,

Welcome to the AutoIt forum.

A simple way is to use a RegEx like this:

#include <MsgBoxConstants.au3>
$sSerialStringExample1 = "ABCDEF123456"
$sDelimiters1 = StringRegExpReplace($sSerialStringExample1, "(.{4})(.{4})(.{4})", "$1-$2-$3")
MsgBox($MB_SYSTEMMODAL, "Delimited", $sDelimiters1)

No doubt a RegEx guru will be along shortly to explain why that is not the best way , but it works for me!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

See, I just knew it would happen!

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Global $Regex, $String = "ABCDEF123456"
Global $Pattern = "^(\w{4})(\w{4})(\w{4})$"         ; ^ <-- beginning of the string ... end of string --> $

For $x = 1 To 2 Step 1
    If $x = 2 Then
        $String &= "5_"
    EndIf
    If StringRegExp($String, $Pattern) Then
        $Regex = StringRegExpReplace($String, $Pattern, "$1" & "-" & "$2" & "-" & "$3")
        MsgBox(0, "Result if string is in the defined format", "String =" & $String & _
        @CRLF & "Result =" & $Regex)
    Else
        MsgBox(0, "Result if not", $String & " does not match.")
    EndIf
Next

 

Link to comment
Share on other sites

Let's wrap it into a function:

#include <StringConstants.au3>

ConsoleWrite(_StringDelimiter("ABCDEFGH123456", 2) & @CRLF)


Func _StringDelimiter($strString, $intNumber)
    Return Mod(StringLen($strString), $intNumber) = 0 ? StringRegExpReplace($strString, '\w{' & $intNumber & '}\K(?!$)', '-') : ''
EndFunc

:)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

22 hours ago, Zero0 said:

Are there any other examples without the loop? Except StringRegExpReplace.

... through recursion

MsgBox(0, '', _stringsInsertDelims('ABCDEF123456', 4))

; inserts delimiters after each group of nr. characters and returns the resulting string
Func _stringsInsertDelims($sString, $iGroupsLen = 1, $sDelim = '-')
    If Number($iGroupsLen) < 1 Then Return SetError(1, 0, $sString)
    Local $sSnippt = StringLeft($sString, $iGroupsLen)
    Local $sReminder = StringTrimLeft($sString, $iGroupsLen)
    If $sReminder <> '' Then $sSnippt &= $sDelim & _stringsInsertDelims($sReminder, $iGroupsLen, $sDelim)
    Return $sSnippt
EndFunc   ;==>_stringsInsertDelims

 

Edited by Chimp
added 3th parameter in recursion call

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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...