编程乐趣:一个强制换行的小方法
发布日期:2021-11-07 06:40:57
浏览次数:13
分类:技术文章
本文共 2401 字,大约阅读时间需要 8 分钟。
前段时间做了个功能,用打印机打印内容。打印是这种格式:
项目名:内容; 由于打印的使用的那种纸宽度有限,所以要考虑到换行于是写了个给字符串换行的功能,试了一下貌似还能用,分享分享,希望能有所帮助。////// 格式化字符格式 /// /// 内容 /// 题目 /// 每行长度 ///private static string FormatPrintLine(string printContent, string itemName, int lineLength) { List contentRows = new List (); 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(); }
效果:
代码下载:
转载地址:https://blog.csdn.net/yysyangyangyangshan/article/details/7976205 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
不错!
[***.144.177.141]2024年02月28日 20时02分40秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
xlnt库如何编译_最新mysql数据库源码编译安装。
2019-04-21
mysql 2003错误 10055_MYSQL无法连接---提示10055错误
2019-04-21
mysql redis缓存层_redis实现缓存的两种方式
2019-04-21
git 改local branch名字_用Git管理Latex写论文的工作流程
2019-04-21
mysql索引篇_MySQL索引篇
2019-04-21
有至少一个用MySQL_Mysql有用的面试题
2019-04-21
mysql select同时update_MySQLSELECT同时UPDATE同一张表
2019-04-21
mysql删除后数据库没变化_mysql之delete删除记录后数据库大小不变
2019-04-21
python问题描述怎么写_python写文件有时候写不进去怎么办
2019-04-21
qpython3安装lxml_在python的lxml中使用xml目录?
2019-04-21
java 幂取模_快速幂取模算法
2019-04-21
java上传下载源码_javaweb简单实现文件上传与下载源代码
2019-04-21
java控制热敏打印机的例子.rar_stm32控制热敏打印机
2019-04-21
java clone equals_(原)java中对象复制、==、equals
2019-04-21
java滚动字幕实训报告_Java实习报告 (7000字).doc
2019-04-21