I have the an array called $Map that has 29 strings of 23 characters. With the $CharY variable I specify the index of an item and with the $CharX variable I specify which character of that item should be replaced. How do I actually replace this character of the item with the character "0"? I tried using$Map[$CharY][CharX]
but after running this command, an error is shown saying
At line:1 char:1
+ $Map[$CharY][$CharX] = "0"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : CannotIndex
.NET strings, which PowerShell uses, are immutable. You can read an individual character by index, but you can't modify it directly. Instead, create a new string with the modified character and assign it back to the array:
Output:
Explanations:
$Map
with 29 strings consisting of 23 characters each.String.ToCharArray()
is used to convert a string into a character array which can be modified.-join
operator is used to create a new string from the individual characters. Alternatively the String constructor that accepts a char array could be used:[String]::new($Line)
.