Remove xA0 from powershell string -
i see few articles on removing xa0 character string in python, i'm not unfamiliar with, tips there not seem work powershell.
my problem i'm parsing excel file did 'ctrl+space' , created xa0 invisible character. i've removed excel sheet, i'm interested in knowing how filter/remove out these characters in general.
it causes problems when exporting these strings xml (doesn't characters).
if non-breaking space, can use -replace
operator replace it:
ps c:\> $s = [string]::join([char]0x00a0, ('hello','world')) ps c:\> $s hello world ps c:\> $s -replace [char]0x00a0,'-' hello-world
you might want replacement after creating xml:
ps c:\> ([psobject]@{"name"=$s} | convertto-xml -as string) -replace [char]0xa0,' ' <?xml version="1.0"?> <objects> <object type="system.collections.hashtable"> <property name="key" type="system.string">name</property> <property name="value" type="system.string">hello world</property> </object> </objects>
or more complex replacement handle non-ascii characters:
ps c:\> $s = [string]::join([char]160, ("hello","powershell","world", "♥♥♥")) ps c:\> $myxml = $s | convertto-xml -as string ps c:\> ([regex]"[\u0080-\uffff]").replace($myxml, { param($m) "&#$([int][char]$m.value);" }) <?xml version="1.0"?> <objects> <object type="system.string">hello powershell world ♥♥♥</object> </objects> ps c:\>
Comments
Post a Comment