C# 截取中英文混合字符串分行显示宽度相同

简单的东东,网上参考借鉴的,什么也不说了,上代码。

View Code
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

public partial class GetPrintContent : System.Web.UI.Page
{
protected string ClientCode = string.Empty;
protected string LogoTxt = string.Empty;
protected void Page_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(Request["Str"]))
{
StringBuilder xmlString = new StringBuilder();
this.ClientCode = "胜多负少胜多负少胜多负少胜多负少12345胜多负少胜多负少胜多负少胜多负少12345胜多负少";
xmlString.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
xmlString.AppendLine("<PRINT>");
xmlString.AppendLine("<LOGO URI=\"中英文混合字符串截取固定长度\"/>");
string logoTxt = this.ClientCode.Replace("\r\n", "");
if ((logoTxt.Length / 16 + 1) > 1)
{
int startIndex = 0;//字节数
int subLen = 16;
int subCount = logoTxt.Length / 16 + 1;
for (int i = 0; i < subCount; i++)
{
string oneOfVal = SubStrLenth(logoTxt, startIndex, subLen * 2);
if (oneOfVal != "")
{
xmlString.AppendLine("<TEXT VAL=\"" + oneOfVal + "\"/>");
startIndex += GetStrByteLength(oneOfVal);
}
else
{
break;
}

}
}
else
{
xmlString.AppendLine("<TEXT VAL=\"" + logoTxt + "\"/>");
}

xmlString.AppendLine("</PRINT>");


Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "text/xml";
Response.Charset = "utf-8";
Response.Write(xmlString);
Response.End();
//}
}
}

private bool ISChinese(char C)
{
return (int)C >= 0x4E00 && (int)C <= 0x9FA5;
}

private int GetStrByteLength(string str)
{
return System.Text.Encoding.Default.GetByteCount(str);
}
private string SubStrLenth(string str, int startIndex, int length)
{
int strlen = GetStrByteLength(str);
if (startIndex + 1 > strlen)
{
return "";
}
int j = 0;//记录遍历的字节数
int L = 0;//记录每次截取开始,遍历到开始的字节位,才开始记字节数
int strW = 0;//字符宽度
bool b = false;//当每次截取时,遍历到开始截取的位置才为true
string restr = string.Empty;
for (int i = 0; i < str.Length; i++)
{
char C = str[i];
if (ISChinese(C))
{
strW = 2;
}
else
{
strW = 1;
}
if ((L == length-1) && (L + strW > length))
{
b = false;
break;
}
if (j >= startIndex)
{
restr += C;
b = true;
}

j += strW;

if (b)
{
L += strW;
if (((L + 1) > length))
{
b = false;
break;
}
}

}
return restr;
}

}

希望有更简洁方便的解决方案的同道,不吝赐教。

最近自己总结 ,结合文友所说,做了缩减,如下,如不对请指正:

 

/// <summary>

        /// 截取方法名显示宽度

        /// </summary>

        /// <param name="methodName">方法名</param>

        /// <param name="byteLen">需要截取的字节长度(可以显示的最长字节长度,包括省略号...)</param>

        /// <returns></returns>

        private string SubMethodName(string methodName,int byteLen)

        {

            int methodLen = Encoding.Default.GetByteCount(methodName);

            if(byteLen<1)

            {

                return methodName;

            }

            if (methodLen <= byteLen)

            {

                return methodName;

            }

            else

            {

                int bytecounter = 0;

                string CurrSubStr = string.Empty;

                for (int i = 0; i < methodName.Length; i++)

                {

                    bytecounter = bytecounter + Encoding.Default.GetByteCount(methodName[i].ToString());

                    if (bytecounter > byteLen-4)

                    {

                        break;

                    }

                    CurrSubStr += methodName[i];

                }

                return CurrSubStr + "...";

            }

        }

 


/// <summary>

        /// 截取方法名显示宽度

        /// </summary>

        /// <param name="methodName">方法名</param>

        /// <param name="byteLen">需要截取的字节长度(可以显示的最长字节长度,以全汉字计算,此值只能为偶数,包括省略号...)</param>

        /// <returns></returns>

        private string SubMethodName(string methodName,int byteLen)

        {

            int methodLen = Encoding.Default.GetByteCount(methodName);

            if(byteLen<1)

            {

                return methodName;

            }

            if (methodLen <= byteLen)

            {

                return methodName;

            }

            else

            {

                int bytecounter = 0;

                string CurrSubStr = string.Empty;

                for (int i = 0; i < methodName.Length; i++)

                {

                    bytecounter = bytecounter + Encoding.Default.GetByteCount(methodName[i].ToString());

                    if (bytecounter > byteLen-4)

                    {

                        break;

                    }

                    CurrSubStr += methodName[i];

                }

                return CurrSubStr + "...";

            }

        }

你可能感兴趣的:(字符串)