Welcome › Forums › General PowerShell Q&A › encode string help
- This topic has 5 replies, 3 voices, and was last updated 9 months, 3 weeks ago by
Participant.
-
AuthorPosts
-
-
April 7, 2020 at 10:48 am #216246
Hi all,
Hope you’re all fine and safe at home 😉
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.
-
April 7, 2020 at 11:09 am #216249
Nothing really sophisticated or elegant or flexible but working. Is it always the same structure of the string or does it vary?
PowerShell1'AZERTY123456789012345' -replace '(?<=AZERTY\d{4})\d+(?=\d{4})',('X'*('AZERTY123456789012345'.Length -14))-
April 7, 2020 at 1:32 pm #216279
Hi Olaf,
thanks for your answers 🙂
the structure of string could vary (some caracters or not before digits) and numbers can count 15 or 16 or 18.
Mika.
-
-
April 7, 2020 at 1:28 pm #216273PowerShell12345678910111213141516171819202122232425262728function 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:
PowerShell123Encode-String 'AZERTY1234567890123456' -Verbose # 16VERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '1234567890123456'AZERTY1234XXXXXXXX3456It can handle different count of digits in the beginning (Prefix) as in:
PowerShell123Encode-String -DTR 'AZERTY123456789012345' -CountOfPrefixDigitsToKeep 6 -VerboseVERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'AZERTY123456XXXXX2345or the end (Suffix) as in:
PowerShell123Encode-String -DTR 'AZERTY123456789012345' -CountOfSuffixDigitsToKeep 7 -VerboseVERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'AZERTY1234XXXX9012345Notice that this function does not care where the digits are located in the string. For example, ‘AZERTY123456789012345’ and ‘1A2Z3E4R5T6Y789012345’ will produce the same output:
PowerShell1234567Encode-String 'AZERTY123456789012345' -VerboseVERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'AZERTY1234XXXXXXX2345Encode-String '1A2Z3E4R5T6Y789012345' -VerboseVERBOSE: Identified Non-numerical part as 'AZERTY', and Numerical part '123456789012345'AZERTY1234XXXXXXX2345Finally, this function does not do any error checking. In a production environment you may want to anticipate all manners of bad input, like
PowerShell123456789Encode-String -DTR 'AZERTY123456789012345' -CountOfPrefixDigitsToKeep 16 -VerboseVERBOSE: 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-
This reply was modified 9 months, 3 weeks ago by
Sam Boutros.
-
This reply was modified 9 months, 3 weeks ago by
-
April 7, 2020 at 1:34 pm #216282
Hi Sam,
wooh it’s so much than I need, it’s amazing 😉
Thanks a lot both of us for your help.
Mika
-
-
AuthorPosts
- The topic ‘encode string help’ is closed to new replies.