如何使用 Regex 或 StringBuilder 转义外来字符?
我有以下清理字符串的方法:
public static String UseStringBuilderWithHashSet(string strIn)
{
var hashSet = new HashSet<char>("?&^$#@!()+-,:;<>’\'-_*");
// specify capacity of StringBuilder to avoid resizing
StringBuilder sb = new StringBuilder(strIn.Length);
foreach (char x in strIn.Where(c => !hashSet.Contains(c)))
{
sb.Append(x);
}
return sb.ToString();
}
但是,[mv] reolちるちるchiruchiru
或[mv] reol reolヒビカセhibikase
没有清理。
如何修改我的方法,以便将上述字符串之一变成: [MV] Reol Chiruchiru
I have the following method to clean up strings:
public static String UseStringBuilderWithHashSet(string strIn)
{
var hashSet = new HashSet<char>("?&^$#@!()+-,:;<>’\'-_*");
// specify capacity of StringBuilder to avoid resizing
StringBuilder sb = new StringBuilder(strIn.Length);
foreach (char x in strIn.Where(c => !hashSet.Contains(c)))
{
sb.Append(x);
}
return sb.ToString();
}
However, strings such as [MV] REOL ちるちる ChiruChiru
or [MV] REOL ヒビカセ Hibikase
do not get cleaned up.
How can I modify my method so it can turn one of the above strings into for example:[MV] REOL ChiruChiru
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你试图通过过滤掉你不想要的一切来彻底解决这个问题。这不是最佳选择,因为它们有 100,000 多个可能的字符。
如果你只接受你想要的,你可能会找到更好的结果。
You're trying to solve this exhaustively by filtering out everything you don't want. This is not optimal as their are 100,000+ possible characters.
You may find better results if you only accept what you do want.