编程乐趣:一个强制换行的小方法
2012-09-13 18:42
295 查看
前段时间做了个功能,用打印机打印内容。打印是这种格式:
项目名:内容;
由于打印的使用的那种纸宽度有限,所以要考虑到换行于是写了个给字符串换行的功能,试了一下貌似还能用,分享分享,希望能有所帮助。
代码下载:http://download.csdn.net/detail/yysyangyangyangshan/4568386
本文出自 “西北白杨树” 博客,请务必保留此出处http://yangyoushan.blog.51cto.com/7229571/1276449
项目名:内容;
由于打印的使用的那种纸宽度有限,所以要考虑到换行于是写了个给字符串换行的功能,试了一下貌似还能用,分享分享,希望能有所帮助。
/// <summary> /// 格式化字符格式 /// </summary> /// <param name="printContent">内容</param> /// <param name="itemName">题目</param> /// <param name="lineLength">每行长度</param> /// <returns></returns> private static string FormatPrintLine(string printContent, string itemName, int lineLength) { List<string> contentRows = new List<string>(); string formathContent = string.Empty; if (printContent.Length <= lineLength) { return itemName + printContent; } int index = 0; string space = string.Empty; for (int i = 0; i < itemName.Length; i++) { Match match = new Regex(@"[\u4E00-\u9FA5]|[\uFE30-\uFFA0]", RegexOptions.IgnoreCase).Match(itemName[i].ToString()); space += match.Success ? " " : " ";//中文占2字符 } do { if (contentRows.Count > 0) { if (((printContent.Length - (contentRows.Count * lineLength))) / lineLength > 0) { contentRows.Add(space + printContent.Substring(contentRows.Count * lineLength, lineLength) + Environment.NewLine); } else { contentRows.Add(space + printContent.Substring(contentRows.Count * lineLength, printContent.Length - (contentRows.Count * lineLength)) + Environment.NewLine); } } else { contentRows.Add(itemName + printContent.Substring(0, lineLength) + Environment.NewLine); } index += lineLength; } while (index < printContent.Length); for (int i = 0; i < contentRows.Count; i++) { formathContent += contentRows[i]; } return formathContent; }调用:
static void Main(string[] args) { string itemName = "内容:"; string content = @" 巴西国家队昨天打进的中国国家队的所有进球完全是非法的、无效的,丝毫改变不了中国球员实力超群的事实,"+ " 丝毫改变不了中国队在国际足坛的霸主地位。中国球员强烈敦促巴西国家队立即停止一切损害中国球门的行为,如果巴西国家队一意孤行,"+ "由此在场上中国球员所造成的一切严重后果只能由巴西国家队承担。"; string result = FormatPrintLine(content,itemName,20); Console.WriteLine(result); Console.ReadKey(); }效果:
代码下载:http://download.csdn.net/detail/yysyangyangyangshan/4568386
本文出自 “西北白杨树” 博客,请务必保留此出处http://yangyoushan.blog.51cto.com/7229571/1276449
相关文章推荐
- 编程乐趣:一个强制换行的小方法
- Java 编程下通过反射获取一个类中的方法名和对应参数名
- 我的编程学习日志(13)--打开一个文件的方法(system(),ShellExecute())
- 编程实现一个堆栈类Stack,要有push()压栈方法,pop()出栈方法,clear()方法
- 10个方法提高编程生产力--一个优秀程序员的忠告
- C++第5周任务【任务2】本任务将设计一个简单的分数类,完成对分数的几个运算。一则巩固基于对象编程的方法,二则也为第10章做运算符重载等积累些感性认识。
- Android编程实现在Activity中操作刷新另外一个Activity数据列表的方法
- 定义一个计算机类:属性:品牌;价格;颜色; 方法:(1)编程的功能 (2)上网的功能 实例化两个对象:“lenovo”;“hasee”
- 编程乐趣:身份证号码验证的方法
- css 不换行 自动换行 强制换行的实现方法
- Java菜鸟编程 String类的一些方法和一个练习
- C++中提供了多种基本的数据类型。实际上,这些远不能满足我们的需求,如复数(第10章的例子大多是处理虚数的),再如分数。本任务将设计一个简单的分数类,完成对分数的几个运算。一则巩固基于对象编程的方法,
- * 1.创建一个Rectangle类,添加width和height两个成员变量。 * 2.在Rectangle中添加两种方法分别计算矩形的周长和面积 * 3.编程利用Rectangle输出一个矩形
- DIV布局强制英文换行(div英文不怎么给力啊~ 只有用别的方法啦)
- 看到的一个可以为 自动化测试编程的一个方法
- Android编程实现在一个程序中启动另一个程序的方法
- 并编程对派出类的功能进行验证。。 轿车类等 派生出自行车类 停车等方法。然后从该类为基类 最大速度。重量等字段以及开车 要求具有速度 定义一个车辆类
- 1.创建一个Rectangle类,添加width和height两个成员变量。 2.在Rectangle中添加两种方法分别计算矩形的周长和面积 3.编程利用Rectangle输出一个矩形的周
- 一个Struts的莫名问题解决方法:Attribute class invalid for tag present according to TLD - 巴士飞扬-专注编程,网站,专业技术
- 1.创建一个Rectangle类,添加width和height两个成员变量。 2.在Rectangle中添加两种方法分别计算矩形的周长和面积 3.编程利用Rectangle输出一个矩形的周