字符串宽相同

 private static string stringformat(string value, int Len)
    {
        string result = "";
        result = GetSubString(value, Len);

        int rlen = System.Text.Encoding.Default.GetByteCount(result);
        int nlen = (Len * 2) + 3;
        if (rlen < nlen)
        {
            for (int j = 0; j < (nlen - rlen); j++)
            {
                result = result + "&nbsp;";
            }
        }
        return result;
    }

    public static string GetSubString(string value, int Len)
    {
        if (value.Length < Len)
            return value;

        int halfwordcount = 0;

        for (int i = 0; i < Len; i++)
        {
            if (CheckHalfWord(value[i].ToString()))
            {
                halfwordcount++;
            }
        }
        if (halfwordcount == 0)
        {
            return value.Substring(0, Len) + "...";
        }
        else
        {
            if (value.Length > Len + halfwordcount / 2)
                return value.Substring(0, Len) + GetSubString(value.Substring(Len), halfwordcount / 2);
            else
                return value;
        }
    }

    private static bool CheckHalfWord(string value)
    {
        if (1 == System.Text.Encoding.Default.GetByteCount(value))
        {
            return true;
        }
        else
        {
            return false;
        }
    }

posted @ 2012-06-20 08:38  洗耳恭听兼烂笔头  阅读(148)  评论(0编辑  收藏  举报