代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
截取中英文混合字符串分行显示宽度相同
作者:
Dezai.CN
/ 发布于
2012/3/26
/
444
<div>using System;</div> <div>using System.Collections.Generic;</div> <div>using System.Web;</div> <div>using System.Web.UI;</div> <div>using System.Web.UI.WebControls;</div> <div>using System.Data;</div> <div>using System.Text;</div> <div></div> <div>public partial class GetPrintContent : System.Web.UI.Page</div> <div>{</div> <div> protected string ClientCode = string.Empty;</div> <div> protected string LogoTxt = string.Empty;</div> <div> protected void Page_Load(object sender, EventArgs e)</div> <div> {</div> <div> if (!string.IsNullOrEmpty(Request["Str"]))</div> <div> {</div> <div> StringBuilder xmlString = new StringBuilder();</div> <div> this.ClientCode = "胜多负少胜多负少胜多负少胜多负少12345胜多负少胜多负少胜多负少胜多负少12345胜多负少";</div> <div> xmlString.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");</div> <div> xmlString.AppendLine("<PRINT>");</div> <div> xmlString.AppendLine("<LOGO URI=\"中英文混合字符串截取固定长度\"/>");</div> <div> string logoTxt = this.ClientCode.Replace("\r\n", "");</div> <div> if ((logoTxt.Length / 16 + 1) > 1)</div> <div> {</div> <div> int startIndex = 0;//字节数</div> <div> int subLen = 16;</div> <div> int subCount = logoTxt.Length / 16 + 1;</div> <div> for (int i = 0; i < subCount; i++)</div> <div> {</div> <div> string oneOfVal = SubStrLenth(logoTxt, startIndex, subLen * 2);</div> <div> if (oneOfVal != "")</div> <div> {</div> <div> xmlString.AppendLine("<TEXT VAL=\"" + oneOfVal + "\"/>");</div> <div> startIndex += GetStrByteLength(oneOfVal);</div> <div> } <div> else</div> <div> {</div> <div> break;</div> <div> } <div> </div> <div> } <div> } <div> else</div> <div> {</div> <div> xmlString.AppendLine("<TEXT VAL=\"" + logoTxt + "\"/>");</div> <div> } <div></div> <div> xmlString.AppendLine("</PRINT>");</div> <div> </div> <div> </div> <div> Response.ContentEncoding = Encoding.UTF8;</div> <div> Response.ContentType = "text/xml";</div> <div> Response.Charset = "utf-8";</div> <div> Response.Write(xmlString);</div> <div> Response.End();</div> <div> //} <div> } <div> } <div></div> <div> private bool ISChinese(char C)</div> <div> {</div> <div> return (int)C >= 0x4E00 && (int)C <= 0x9FA5;</div> <div> } <div></div> <div> private int GetStrByteLength(string str)</div> <div> {</div> <div> return System.Text.Encoding.Default.GetByteCount(str);</div> <div> } <div> private string SubStrLenth(string str, int startIndex, int length)</div> <div> {</div> <div> int strlen = GetStrByteLength(str);</div> <div> if (startIndex + 1 > strlen)</div> <div> {</div> <div> return "";</div> <div> } <div> int j = 0;//记录遍历的字节数</div> <div> int L = 0;//记录每次截取开始,遍历到开始的字节位,才开始记字节数</div> <div> int strW = 0;//字符宽度</div> <div> bool b = false;//当每次截取时,遍历到开始截取的位置才为true</div> <div> string restr = string.Empty;</div> <div> for (int i = 0; i < str.Length; i++)</div> <div> {</div> <div> char C = str[i];</div> <div> if (ISChinese(C))</div> <div> {</div> <div> strW = 2;</div> <div> } <div> else</div> <div> {</div> <div> strW = 1;</div> <div> } <div> if ((L == length-1) && (L + strW > length))</div> <div> {</div> <div> b = false;</div> <div> break;</div> <div> } <div> if (j >= startIndex)</div> <div> {</div> <div> restr += C;</div> <div> b = true;</div> <div> } <div> </div> <div> j += strW;</div> <div></div> <div> if (b)</div> <div> {</div> <div> L += strW;</div> <div> if (((L + 1) > length))</div> <div> {</div> <div> b = false;</div> <div> break;</div> <div> } <div> } <div></div> <div> } <div> return restr;</div> <div> } <div></div> <div>}
试试其它关键字
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3