encode string help

Hi all,

Hope you’re all fine and safe at home :wink:

I subscribe here because I need help.

I need to encode a string like :

AZERTY123456789012345 to AZERTY1234xxxxxxx2345

So I need keep their 4 first and their 4 last digits, others betwen need to be replace by “x”

I try to find a way to tell : in the string, count between 4th first to 4th last and replace it.

I’ve this run but is it possible to do it by an other way, more efficient and adaptable (like 16-18 digits string) ?

$DTR = “AZERTY123456789012345” #My string need to be encode
$CODE = (“$DTR”).Substring(10,7) #Select between 4first and 4last digits in sub var
$CODE2 = “$CODE” -replace “\d”,“x” # replace digits by “X” in the sub var
$CNUMB = (“$DTR”).Replace(“$CODE”, “$CODE2”) # replace the sub var digits by sub var “x”

Thank you four your help,

Mika.

 

Nothing really sophisticated or elegant or flexible but working. Is it always the same structure of the string or does it vary?

‘AZERTY123456789012345’ -replace ‘(?<=AZERTY\d{4})\d+(?=\d{4})’,(‘X’*(‘AZERTY123456789012345’.Length -14))

function Encode-String {

    [CmdletBinding(ConfirmImpact='Low')] 
    Param(
        [Parameter(Mandatory=$false)][String]$DTR = 'AZERTY123456789012345',
        [Parameter(Mandatory=$false)][Int]$CountOfPrefixDigitsToKeep = 4,
        [Parameter(Mandatory=$false)][Int]$CountOfSuffixDigitsToKeep = 4,
        [Parameter(Mandatory=$false)][Char]$FillerCharacter = 'X'
    )

    Begin { }

    Process {

        # Identify Numerical/non numerical characters
        $NonNumericalPart = ([Char[]]$DTR | foreach { if ([Int]$_ -le 47 -or  [Int]$_ -ge 58) {$_} }) -join '' 
        $NumericalPart    = ([Char[]]$DTR | foreach { if ([Int]$_ -ge 48 -and [Int]$_ -le 57) {$_} }) -join '' 
        Write-Verbose "Identified Non-numerical part as '$NonNumericalPart', and Numerical part '$NumericalPart'"

        # Concatenate and output the desired string
        $NonNumericalPart +
        $NumericalPart.Substring(0,$CountOfPrefixDigitsToKeep) + 
        [String]$FillerCharacter*($NumericalPart.Length-($CountOfPrefixDigitsToKeep+$CountOfSuffixDigitsToKeep)) + 
        $NumericalPart.Substring(($NumericalPart.Length-$CountOfSuffixDigitsToKeep),$CountOfSuffixDigitsToKeep)
    } 

    End { }
}

This will work for different size strings like those containing 15,16,17,18 digits:

Encode-String 'AZERTY1234567890123456' -Verbose # 16
VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '1234567890123456'
AZERTY1234XXXXXXXX3456

It can handle different count of digits in the beginning (Prefix) as in:

Encode-String -DTR 'AZERTY123456789012345' -CountOfPrefixDigitsToKeep 6 -Verbose 
VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'
AZERTY123456XXXXX2345

or the end (Suffix) as in:

Encode-String -DTR 'AZERTY123456789012345' -CountOfSuffixDigitsToKeep 7 -Verbose 
VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'
AZERTY1234XXXX9012345

Notice that this function does not care where the digits are located in the string. For example, ‘AZERTY123456789012345’ and ‘1A2Z3E4R5T6Y789012345’ will produce the same output:

Encode-String 'AZERTY123456789012345' -Verbose 
VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'
AZERTY1234XXXXXXX2345

Encode-String '1A2Z3E4R5T6Y789012345' -Verbose 
VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'
AZERTY1234XXXXXXX2345

Finally, this function does not do any error checking. In a production environment you may want to anticipate all manners of bad input, like

Encode-String -DTR 'AZERTY123456789012345' -CountOfPrefixDigitsToKeep 16 -Verbose 
VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'
Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string.
Parameter name: length"
At line:21 char:9
+         $NonNumericalPart +
+         ~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ArgumentOutOfRangeException

Hi Olaf,

thanks for your answers :slight_smile:

the structure of string could vary (some caracters or not before digits) and numbers can count 15 or 16 or 18.

Mika.

 

Hi Sam,

wooh it’s so much than I need, it’s amazing :wink:

Thanks a lot both of us for your help.

 

Mika