![](/img/trans.png)
[英]String Conversion - remove some characters and replace non-digits with ASCII code
[英]Replace all non ascii characters with a code
是否可以在 c# 字符串中用代码替换所有非 ASCII 字符。 我有一个使用 ZPL 打印到 Zebra 标签打印机的应用程序。 它需要将所有 UTF-8 字符转换为带有前导下划线的代码。 例如,如果用户想要打印 µ(微型符号),我必须这样做
text = text.replace("µ", "_c2_b5"); //c2b5 is the UTF8 code for µ
示例“Helloµ±”应变为“Hello_c2_b5_c2_b1”
这将有助于:
var source = "Helloµ±";
var sb = new StringBuilder();
foreach (char c in source)
{
if (c == '_')
{
// special case: Replace _ With _5f
sb.Append("_5f");
}
else if (c < 32 || c > 127)
{
// handle non-ascii by using hex representation of bytes
// TODO: check whether "surrogate pairs" are handled correctly (if required)
var ba = Encoding.UTF8.GetBytes(new[] { c });
foreach (byte b in ba)
{
sb.AppendFormat("_{0:x2}", b);
}
}
else
{
// in printable ASCII range, so just copy
sb.Append(c);
}
}
Console.WriteLine(sb.ToString());
这导致"Hello_c2_b5_c2_b1"
你可以用一个好的方法来包装它。
后期添加:前两个测试可以组合,因为_
只需用它的字节表示替换,以避免混淆 _ 在结果中的含义:
if (c == '_' || c < 32 || c > 127)
{
var ba = Encoding.UTF8.GetBytes(new[] { c });
foreach (byte b in ba)
{
sb.AppendFormat("_{0:x2}", b);
}
}
else
{
sb.Append(c);
}
你可以试试这个。
var bytes = System.Text.Encoding.ASCII.GetBytes("søme string");
string result = System.Text.Encoding.UTF8.GetString(bytes);
string s = "søme string";
s = Regex.Replace(s, @"[^\u0000-\u007F]+", string.Empty);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.