用字符串中的变音符号替换字符 [英] Replacing characters with diacritics in a string
问题描述
我想去掉一些特殊字符.
Im trying to get rid of some special characters.
在这种情况下,瑞典语 ÅÄÖ 和 åäö.为什么这不起作用?
In this case Swedish ÅÄÖ and åäö. Why is this not working?
结果应该是 AAOaao
但我得到 ÅÄÖåäO
?
The result should be AAOaao
but I get ÅÄÖåäO
?
$stringX = "ÅÄÖåäö"
$stringX = ($stringX -replace "Å$","A")
$stringX = ($stringX -replace "Ä$","A")
$stringX = ($stringX -replace "Ö$","O")
$stringX = ($stringX -replace "å$","a")
$stringX = ($stringX -replace "ä$","a")
$stringX = ($stringX -replace "ö$","o")
"Result = $stringX"
推荐答案
哦!你离得太近了!删除 "Å$"
中的 $
符号对我有用.
Oh! You were so close! Removing the $
sign in the "Å$"
worked for me.
自己试试...
$stringX = "ÅÄÖåäö"
$stringX = ($stringX -replace "Å","A")
$stringX = ($stringX -replace "Ä","A")
$stringX = ($stringX -replace "Ö","O")
$stringX = ($stringX -replace "å","a")
$stringX = ($stringX -replace "ä","a")
$stringX = ($stringX -replace "ö","o")
"Result = $stringX"
正如注释中所指出的,$
表示正则表达式中字符串的结尾.由于您的示例中只有最后一个ö"满足这一点,因此它是唯一一个被替换的.
As pointed out in the comments $
means the end of the string in regex. Since only the final "ö" satisfies this in your sample, it was the only one getting replaced.
还指出,-replace
不区分大小写不敏感,因此您已经有效地复制了您的工作.如果你想替换一切,你只需要
Also pointed out, -replace
is case-insensitive so you've effectively duplicated your work.
If you want to replace everything, you only need
$stringX = ($stringX -replace "Å","A")
$stringX = ($stringX -replace "Ä","A")
$stringX = ($stringX -replace "Ö","O")
如果您只需要替换大写或小写,则使用-creplace
.
If you need to only replace either upper or lower case, then use the -creplace
.
# Only replace the upper case letters...
$stringX = ($stringX -creplace "Å","A")
$stringX = ($stringX -creplace "Ä","A")
$stringX = ($stringX -creplace "Ö","O")
这篇关于用字符串中的变音符号替换字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!