Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace numbers with letters - powershell

I have an array element that I am trying to replace all the numbers with letters. This is the array element before manipulation:

$a[0].object

--- OUTPUT ---
518773112

I convert the number to a string and store it in $e. Then I find the length of that string and store that in $d

$e = $a[0].object.ToString()
$d = $a[0].object.ToString().length
$e
$d

---OUTPUT---
518773112
9

Then I try to looping through the string to replace the numbers with letters. I place $i in the Substring to get the number that I'm trying to replace. Then I use an if-statement to see if that substring is equal to a number, and if so, then I use the replace method to try and replace the number with a letter.

for($i=0; $i -lt $d; $i++){
    $pos = $e.Substring($i,1)
    if($pos -eq '0'){$e += $pos.replace('0', 'A')}
    if($pos -eq '1'){$e += $pos.replace('1', 'B')}
    if($pos -eq '2'){$e += $pos.replace('2', 'C')}
    if($pos -eq '3'){$e += $pos.replace('3', 'D')}
    if($pos -eq '4'){$e += $pos.replace('4', 'E')}
    if($pos -eq '5'){$e += $pos.replace('5', 'F')}
    if($pos -eq '6'){$e += $pos.replace('6', 'G')}
    if($pos -eq '7'){$e += $pos.replace('7', 'H')}
    if($pos -eq '8'){$e += $pos.replace('8', 'I')}
    if($pos -eq '9'){$e += $pos.replace('9', 'J')}
}

My issue though, is it will concatenate the letters to the end of the number string:

518773112FBIHHDBBC

Likely, because I have += in my then portion of my if-statement. However, when I replace += with = I get an error with my Substring method saying:

Exception calling "Substring" with "2" argument(s): "startIndex cannot be larger than length of string.

I've checked $i and $d and they are set correctly before I run the loop. So, I'm a little confused. What am I missing?

like image 377
Crimp Avatar asked Jun 06 '26 14:06

Crimp


1 Answers

To add a concise alternative to the existing, helpful answers, via a single, regex-based
-replace operation:

# PowerShell (Core) 7+ only.
# -> 'FBIHHDBBC'
'518773112' -replace '\d', {  [char] ([int] [char] 'A' + [int] $_.Value) }

# Windows PowerShell alternative:
# (requires direct use of underlying .NET APIs)
[regex]::Replace('518773112', '\d', { param($m) [char] ([int] [char] 'A' + [int] $m.Value) })
  • Regex escape sequence \d matches any single decimal digit.

  • [int] [char] 'A' + [int] $_.Value adds the value of the captured digit ($_.Value) as an integer to the integer representation of character A, i.e. it adds an offset to the Unicode code point of A.

  • The resulting code point is then re-converted to a character ([char]), which yields the letter of interest.


As for what you tried:

Leaving the inefficiency of your approach aside, the problem was that you tried to build your result string in the same variable as the input string:

  • += therefore appended the desired result to the input string
  • Just = essentially replaced the input string with the single character being processed, causing subsequent .Substring() calls to fail, given that the string was now reduced to a single character.

The immediate fix would have been to build the result in a new variable (but note that all the solutions presented in the answers here a preferable):

$e = '518773112'
$d = $e.Length

$result = '' # initialize result string
for($i=0; $i -lt $d; $i++){
  $pos = $e.Substring($i,1)
  # Use += on *$result*
  if($pos -eq '0'){$result += $pos.replace('0', 'A')}
  if($pos -eq '1'){$result += $pos.replace('1', 'B')}
  if($pos -eq '2'){$result += $pos.replace('2', 'C')}
  if($pos -eq '3'){$result += $pos.replace('3', 'D')}
  if($pos -eq '4'){$result += $pos.replace('4', 'E')}
  if($pos -eq '5'){$result += $pos.replace('5', 'F')}
  if($pos -eq '6'){$result += $pos.replace('6', 'G')}
  if($pos -eq '7'){$result += $pos.replace('7', 'H')}
  if($pos -eq '8'){$result += $pos.replace('8', 'I')}
  if($pos -eq '9'){$result += $pos.replace('9', 'J')}
}

$result # Output the result: -> 'FBIHHDBBC'
like image 116
mklement0 Avatar answered Jun 08 '26 09:06

mklement0



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!