程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# 截取中英文混合字符串分行顯示寬度相同

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

編輯: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.2cto.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;
        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;
    }

}

希望有更簡潔方便的解決方案的同道,不吝賜教。


 

摘自  未來證明現在

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved