/// <summary> /// 返回指定字符串中位于指定位置的字符的宽度。 /// </summary> /// <param name="str">要获取宽度的字符字符串。</param> /// <param name="index"><paramref name="str"/> 中的字符位置。</param> /// <returns><paramref name="str"/> 中指定字符的宽度,结果可能是 <c>0</c>、<c>1</c> 或 <c>2</c>。</returns> /// <remarks>此方法基于 Unicode 标准 6.3 版。详情请参见 /// <see href="http://www.unicode.org/reports/tr11/">Unicode Standard Annex #11 EAST ASIAN WIDTH</see>。 /// 返回 <c>0</c> 表示控制字符、非间距字符或格式字符,<c>1</c> 表示半角字符, /// <c>2</c> 表示全角字符。</remarks> /// <exception cref="ArgumentNullException"><paramref name="str"/> 为 <c>null</c>。</exception> /// <exception cref="IndexOutOfRangeException"><paramref name="index"/> 大于等于字符串的长度或小于零。</exception> /// <seealso href="http://www.unicode.org/reports/tr11/">Unicode Standard Annex #11 EAST ASIAN WIDTH</seealso>。 public static int Width(string str, int index) { if (str == null) { throw CommonExceptions.ArgumentNull("str"); } if (index < 0) { throw CommonExceptions.ArgumentNegative("index", index); } if (index >= str.Length) { throw CommonExceptions.ArgumentOutOfRange("index", index); } Contract.Ensures(Contract.Result <int>() >= 0 && Contract.Result <int>() <= 2); return(Width(char.ConvertToUtf32(str, index))); }