转换为字符串/整数在C#中上标 [英] Convert a string/integer to superscript in C#
本文介绍了转换为字符串/整数在C#中上标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个内置的.NET函数或简单的方法来从转换:
Is there a built in .NET function or an easy way to convert from:
"01234"
到
"\u2070\u00B9\u00B2\u00B3\u2074"
请注意,上标1,2和3是不在范围 \\\⁰-\\\ 但\\\-\\\ÿ 。
Note that superscript 1, 2 and 3 are not in the range \u2070-\u209F but \u0080-\u00FF.
推荐答案
编辑:我没有注意到,标字符并没有想象中那么简单 \\\⁰
- \\\⁹
。你可能要设置字符之间的映射。如果你只需要数字,你可以只索引一个字符串相当容易:
I hadn't noticed that the superscript characters weren't as simple as \u2070
-\u2079
. You probably want to set up a mapping between characters. If you only need digits, you could just index into a string fairly easily:
const string SuperscriptDigits =
"\u2070\u00b9\u00b2\u00b3\u2074\u2075\u2076\u2077\u2078\u2079";
然后,使用LINQ:
Then using LINQ:
string superscript = new string(text.Select(x => SuperscriptDigits[x - '0'])
.ToArray());
或者无:
char[] chars = text.ToArray();
for (int i = 0; i < chars.Length; i++)
{
chars[i] = SuperscriptDigits[chars[i] - '0'];
}
string superscript = new string(chars);
这篇关于转换为字符串/整数在C#中上标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文