encode string help

Welcome Forums General PowerShell Q&A encode string help

Viewing 3 reply threads
  • Author
    Posts
    • #216246
      Participant
      Topics: 1
      Replies: 2
      Points: 15
      Rank: Member

      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.

       

    • #216249
      Participant
      Topics: 10
      Replies: 2480
      Points: 6,537
      Helping Hand
      Rank: Community MVP

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

      • #216279
        Participant
        Topics: 1
        Replies: 2
        Points: 15
        Rank: Member

        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.

         

    • #216273
      Participant
      Topics: 12
      Replies: 547
      Points: 1,345
      Helping Hand
      Rank: Community Hero

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

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

      or the end (Suffix) as in:

      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:

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

      • This reply was modified 9 months, 3 weeks ago by Sam Boutros.
    • #216282
      Participant
      Topics: 1
      Replies: 2
      Points: 15
      Rank: Member

      Hi Sam,

      wooh it’s so much than I need, it’s amazing 😉

      Thanks a lot both of us for your help.

       

      Mika

Viewing 3 reply threads
  • The topic ‘encode string help’ is closed to new replies.