当前位置:编程学习 > C#/ASP.NET >>

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;//字节数www.zzzyk.com
                    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;
 

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,