Multiple Text Replace (Fast)

by Jun 30, 2011

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!