0

文字列「H20」(水の化学式)があります。文字列内のすべての数字が小さくなるように変更したいと思います (数字 2 は、文字 H の横にある小さなインデックスになります)。どうやってやるの?

4

2 に答える 2

4
于 2012-01-21T21:48:31.367 に答える
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 に答える