字符串打印自动换行
2013-08-20 16:47
218 查看
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace zidonghuanhang{ class Program { static void Main(string[] args) { string a = AutomaticLine("我们都是小青蛙,呱呱呱呱呱,喜欢快乐的生活最爱说笑话", 20); Console.WriteLine(a);
Console.ReadKey(); } public static string AutomaticLine(string strOldText, int paperWidth) { int intLength; string strResult = ""; intLength = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(strOldText); if (intLength > paperWidth) { byte[] bytes =
System.Text.Encoding.Unicode.GetBytes(strOldText); int intCutPos = RealCutPos(bytes, paperWidth); strResult = System.Text.Encoding.Unicode.GetString(bytes, 0, intCutPos * 2) + Environment.NewLine + AutomaticLine(strOldText.Substring(intCutPos), paperWidth);
} else if (intLength < paperWidth) strResult = strOldText; return strResult; } public static int RealCutPos(byte[] b, int length) { //获取UCS2编码 int intCountB = 0;//统计当前的字节数 int intCutPos = 0;//记录要截取字段的位置 while (intCutPos < b.GetLength(0) && intCountB < length)
{ //偶数位置,如0、2、4等,为UCS2编码中两个字节的第一个字节 if (intCutPos%2 == 0) intCountB++; else { if (b[intCutPos] > 0) intCountB++; } intCutPos++; } if (intCutPos%2 == 1) { if (b[intCutPos] > 0) intCutPos--; else intCutPos++; } return intCutPos/2; } }}
Console.ReadKey(); } public static string AutomaticLine(string strOldText, int paperWidth) { int intLength; string strResult = ""; intLength = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(strOldText); if (intLength > paperWidth) { byte[] bytes =
System.Text.Encoding.Unicode.GetBytes(strOldText); int intCutPos = RealCutPos(bytes, paperWidth); strResult = System.Text.Encoding.Unicode.GetString(bytes, 0, intCutPos * 2) + Environment.NewLine + AutomaticLine(strOldText.Substring(intCutPos), paperWidth);
} else if (intLength < paperWidth) strResult = strOldText; return strResult; } public static int RealCutPos(byte[] b, int length) { //获取UCS2编码 int intCountB = 0;//统计当前的字节数 int intCutPos = 0;//记录要截取字段的位置 while (intCutPos < b.GetLength(0) && intCountB < length)
{ //偶数位置,如0、2、4等,为UCS2编码中两个字节的第一个字节 if (intCutPos%2 == 0) intCountB++; else { if (b[intCutPos] > 0) intCountB++; } intCutPos++; } if (intCutPos%2 == 1) { if (b[intCutPos] > 0) intCutPos--; else intCutPos++; } return intCutPos/2; } }}
相关文章推荐
- PrintWordWarp & WordWarp[打印自动换行,字符串自动换行]
- 利用Word将连着一起的字符按照自己指定的”字符串或者字换行“自动换行。
- 打印小票时自动换行算法,可处理各种字符混排
- html表单样式, table隔行高亮, 长字符串自动换行
- ie8下英文字符串无法自动换行的问题
- TextEdit UI for Long Text Word-break 英文字符串自动换行处理
- Android绘制字符串自动换行
- thinkphp 反字符 去标签 自动加点 去换行 截取字符串
- 页面中,对于长字符串自动换行的做法
- 让英文字符串超出表格宽度自动换行
- div 标签里的内容或字符串自动换行
- 解决页面表格中长英文字符串不能自动换行的问题
- thinkphp 反字符 去标签 自动加点 去换行 截取字符串
- MyEclipse中 将字符串回车自动换行的配置(自己最常用的就是解决在pl/sql工具下美化好的sql语句放入字符串中)
- 打印流PrintWriter实现自动刷新和换行
- Symbian 字符串自动换行
- java 打印例子(字符串换行问题)
- QT for symbian 字符串的自动换行
- div 标签里的内容或字符串自动换行
- 在区域内绘制宽字符串多行自动换行的c语言实现方法