C # truncate the branch of the Chinese and English strings with the same display width

Source: Internet
Author: User

Simple Dongdong, nothing to say, on the code.
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 = "wins, lose, win, lose ";
XmlString. AppendLine ("<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> ");
XmlString. AppendLine ("<PRINT> ");
XmlString. AppendLine ("<logo uri = \" Chinese and English string truncation fixed length \ "/> ");
String logoTxt = this. ClientCode. Replace ("\ r \ n ","");
If (logoTxt. Length/16 + 1)> 1)
{
Int startIndex = 0; // number of bytes 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; // number of bytes that record Traversal
Int L = 0; // records the number of bytes starting from each capture and traversing to the starting byte
Int strW = 0; // character width
Bool B = false; // The value of bool B is true only when the position where the screenshot starts is traversed.
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;
}

}

We hope to share the same path with you for more concise and convenient solutions.


 

From Proof of future

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.