我有字符串“H20”(水的化学式)。我想改变它,使字符串中的所有数字都变小(数字 2 将是字母 H 旁边的小索引)。我怎样才能做到这一点?
问问题
2167 次
2 回答
4
假设您有显示下标 unicode 字符的方法,您可以轻松编写自己的下标扩展方法:
public static string Subscript(this string normal)
{
if(normal == null) return normal;
var res = new StringBuilder();
foreach(var c in normal)
{
char c1 = c;
// I'm not quite sure if char.IsDigit(c) returns true for, for example, '³',
// so I'm using the safe approach here
if (c >= '0' && c <= '9')
{
// 0x208x is the unicode offset of the subscripted number characters
c1 = (char)(c - '0' + 0x2080);
}
res.Append(c1);
}
return res.ToString();
}
于 2012-01-21T21:45:36.970 回答
4
于 2012-01-21T21:48:31.367 回答