Multiple Text Replace (Fast)

Share |

Subscribe to Feed

PowerShell eBook
Sign up for
Your PowerTip of the Day:

Powershell Plus

13 Free SQL Tools from Idera

Free Ebook: Mastering Powershell


In a previous tip, we showed you how to replace multiple different characters in a text using the Switch statement. While this works well, it is not very fast. The following approach is about 150-times faster:

PS> $text = 'Österreich überholt außen Ängland'
PS > $text -creplace 'ä', 'ae' -creplace 'ö', 'oe' -creplace 'ü', 'ue' -creplace 'Ä', 'Äe' -creplace 'Ö', 'Oe' -creplace 'Ü', 'Ue' -creplace 'ß', 'ss'


And if you do not need regular expressions, this approach will double  the speed again:

PS > $text.Replace('ä', 'ae').Replace('ö', 'oe').Replace('ü', 'ue').Replace('Ä', 'Ae').Replace('Ö', 'Oe').Replace('Ü', 'Ue').Replace('ß', 'ss')


Twitter This Tip! ReTweet this Tip!

Posted Jun 30 2011, 08:00 AM by ps1
Filed under: ,
Copyright 2012 All rights reserved.