Multiple Text Replace

by Jun 23, 2011

Imagine that you need to replace a number of different characters in a text. For example, you need to remove special characters or escape something. The switch statement can do that. You will just need to temporarily convert the text into a character array:

PS> $text = 'Österreich überholt außen Ängland'

PS> -join $(switch -case ( [Char[]]$text ) {
  ä { 'ae' }
  ö { 'oe' }
  ü { 'ue' }
  Ä { 'Ae' }
  Ö { 'Oe' }
  Ü { 'Ue' }
  ß { 'ss' }
  default { $_ }
})

Oesterreich ueberholt aussen Aengland

 

Twitter This Tip!
ReTweet this Tip!