当前位置:首页 > Windows程序 > 正文

一个用C#写的删除字符串中回车、换行、制表符、空格的程序

2021-05-25 Windows程序

在使用知网CAJViewer(我用的版本是7.2.0 Build 111)查阅文献时,,将文本复制Notepad中会产生多个换行,如下图所示:

技术分享

毫无疑问,手工删除这里面的回车(\n)、换行(\r)、制表符(\t)、空格都删去,是非常费时费力的。大约一个月前,我用C#写了一个非常简易的小工具来解决这个问题,今天我把这个工具的代码记录下来,方便日后使用。

程序界面如图:

技术分享


这个程序的窗口被设定为总在最前,将CAJViewer中【选择文本】状态选中的文字,按Ctrl+C复制后,在程序文本编辑界面按下Ctrl+V粘贴,点击【转换】按钮,程序将自动删去回车、换行、制表符、空格四类字符,点击【复制】可以将新生成的文字直接复制到剪贴板,点击【重置】可以将文字编辑界面置空。为了使用方便,我还专门为这三个按钮设置了快捷键(Alt+Z、Alt+X、Alt+C),使用时自左至右依次按过即可完成一套从CAJViewer中复制粘贴的Combo!

程序界面:

技术分享

程序代码:FormMain.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ReturnKiller {     public partial class FormMain : Form     {         public FormMain()         {             InitializeComponent();         }         private void btnTrans_Click(object sender, EventArgs e)         {             string strTemp = txtText.Text;             strTemp = strTemp.Replace("\r", "");             strTemp = strTemp.Replace("\n", "");             strTemp = strTemp.Replace("\t", "");             strTemp = strTemp.Replace(" ", "");             txtText.Text = strTemp;         }         private void btnCopy_Click(object sender, EventArgs e)         {             Clipboard.Clear();             if (!string.IsNullOrWhiteSpace(txtText.Text))             {                 Clipboard.SetText(txtText.Text);                 Console.Beep(10000, 5);             }         }         private void btnReset_Click(object sender, EventArgs e)         {             txtText.Text = "";         }     } }

设计器代码:

namespace ReturnKiller {     partial class FormMain     {         /// <summary>         /// 必需的设计器变量。         /// </summary>         private System.ComponentModel.IContainer components = null;         /// <summary>         /// 清理所有正在使用的资源。         /// </summary>         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>         protected override void Dispose(bool disposing)         {             if (disposing && (components != null))             {                 components.Dispose();             }             base.Dispose(disposing);         }         #region Windows 窗体设计器生成的代码         /// <summary>         /// 设计器支持所需的方法 - 不要         /// 使用代码编辑器修改此方法的内容。         /// </summary>         private void InitializeComponent()         {             this.panel2 = new System.Windows.Forms.Panel();             this.label1 = new System.Windows.Forms.Label();             this.btnReset = new System.Windows.Forms.Button();             this.btnTrans = new System.Windows.Forms.Button();             this.groupBox1 = new System.Windows.Forms.GroupBox();             this.txtText = new System.Windows.Forms.TextBox();             this.btnCopy = new System.Windows.Forms.Button();             this.panel2.SuspendLayout();             this.groupBox1.SuspendLayout();             this.SuspendLayout();             //              // panel2             //              this.panel2.Controls.Add(this.btnCopy);             this.panel2.Controls.Add(this.label1);             this.panel2.Controls.Add(this.btnReset);             this.panel2.Controls.Add(this.btnTrans);             this.panel2.Dock = System.Windows.Forms.DockStyle.Bottom;             this.panel2.Location = new System.Drawing.Point(0, 233);             this.panel2.Name = "panel2";             this.panel2.Size = new System.Drawing.Size(478, 31);             this.panel2.TabIndex = 1;             //              // label1             //              this.label1.AutoSize = true;             this.label1.Location = new System.Drawing.Point(301, 9);             this.label1.Name = "label1";             this.label1.Size = new System.Drawing.Size(167, 12);             this.label1.TabIndex = 2;             this.label1.Text = "Tsybius 制作于 2015年5月3日";             //              // btnReset             //              this.btnReset.Location = new System.Drawing.Point(169, 4);             this.btnReset.Name = "btnReset";             this.btnReset.Size = new System.Drawing.Size(75, 23);             this.btnReset.TabIndex = 1;             this.btnReset.Text = "重置(&c)";             this.btnReset.UseVisualStyleBackColor = true;             this.btnReset.Click += new System.EventHandler(this.btnReset_Click);             //              // btnTrans             //              this.btnTrans.Location = new System.Drawing.Point(9, 4);             this.btnTrans.Name = "btnTrans";             this.btnTrans.Size = new System.Drawing.Size(75, 23);             this.btnTrans.TabIndex = 0;             this.btnTrans.Text = "转换(&z)";             this.btnTrans.UseVisualStyleBackColor = true;             this.btnTrans.Click += new System.EventHandler(this.btnTrans_Click);             //              // groupBox1             //              this.groupBox1.Controls.Add(this.txtText);             this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;             this.groupBox1.Location = new System.Drawing.Point(0, 0);             this.groupBox1.Name = "groupBox1";             this.groupBox1.Size = new System.Drawing.Size(478, 233);             this.groupBox1.TabIndex = 2;             this.groupBox1.TabStop = false;             this.groupBox1.Text = "此处输入正文";             //              // txtText             //              this.txtText.Dock = System.Windows.Forms.DockStyle.Fill;             this.txtText.Location = new System.Drawing.Point(3, 17);             this.txtText.Multiline = true;             this.txtText.Name = "txtText";             this.txtText.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;             this.txtText.Size = new System.Drawing.Size(472, 213);             this.txtText.TabIndex = 0;             //              // btnCopy             //              this.btnCopy.Location = new System.Drawing.Point(89, 4);             this.btnCopy.Name = "btnCopy";             this.btnCopy.Size = new System.Drawing.Size(75, 23);             this.btnCopy.TabIndex = 3;             this.btnCopy.Text = "复制(&x)";             this.btnCopy.UseVisualStyleBackColor = true;             this.btnCopy.Click += new System.EventHandler(this.btnCopy_Click);             //              // FormMain             //              this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;             this.ClientSize = new System.Drawing.Size(478, 264);             this.Controls.Add(this.groupBox1);             this.Controls.Add(this.panel2);             this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;             this.MaximizeBox = false;             this.Name = "FormMain";             this.ShowIcon = false;             this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;             this.Text = "ReturnKiller";             this.TopMost = true;             this.panel2.ResumeLayout(false);             this.panel2.PerformLayout();             this.groupBox1.ResumeLayout(false);             this.groupBox1.PerformLayout();             this.ResumeLayout(false);         }         #endregion         private System.Windows.Forms.Panel panel2;         private System.Windows.Forms.Button btnReset;         private System.Windows.Forms.Button btnTrans;         private System.Windows.Forms.GroupBox groupBox1;         private System.Windows.Forms.TextBox txtText;         private System.Windows.Forms.Label label1;         private System.Windows.Forms.Button btnCopy;     } }


附:工程文件的下载地址(百度网盘) 

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70979.html