r/PowerShell • u/ScubaFett • Jan 20 '26
Solved Replacing the nth instance of a character?
Is there a way to replace say the 3rd space in a string to a dash?:
The quick brown fox jumped over the lazy dog
becomes
The quick brown**-**fox jumped over the lazy dog
I'm doing this with file names so the words differ, otherwise I would do:$FileName = $FileName.Replace("brown fox","brown-fox")
Looking to avoid using split on space and then rejoining the text including the dash, or counting to the ~15th character etc. TIA
3
Upvotes
View all comments
Show parent comment
3
u/charleswj Jan 20 '26
You just replace every instance of a space with your new character. I didn't test, but I'm pretty sure you can do multi character strings as well.
I'm on mobile, so I hope I didn't mess it up, but I added variables below to try to make it more obvious