一個用C#寫的刪除字符串中回車、換行、製表符、空格的程序

在使用知網CAJViewer(我用的版本是7.2.0 Build 111)查閱文獻時,將文本複製Notepad中會產生多個換行,以下圖所示:編輯器

毫無疑問,手工刪除這裏面的回車(\n)、換行(\r)、製表符(\t)、空格都刪去,是很是費時費力的。大約一個月前,我用C#寫了一個很是簡易的小工具來解決這個問題,今天我把這個工具的代碼記錄下來,方便往後使用。ide

程序界面如圖:工具

這個程序的窗口被設定爲總在最前,將CAJViewer中【選擇文本】狀態選中的文字,按Ctrl+C複製後,在程序文本編輯界面按下Ctrl+V粘貼,點擊【轉換】按鈕,程序將自動刪去回車、換行、製表符、空格四類字符,點擊【複製】能夠將新生成的文字直接複製到剪貼板,點擊【重置】能夠將文字編輯界面置空。爲了使用方便,我還專門爲這三個按鈕設置了快捷鍵(Alt+Z、Alt+X、Alt+C),使用時自左至右依次按過便可完成一套從CAJViewer中複製粘貼的Combo!ui


程序界面:this

程序代碼:FormMain.csspa

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;

    }
}

附:工程文件的下載地址(百度網盤)http://pan.baidu.com/s/1sj3aI7r code

ENDcomponent

相關文章
相關標籤/搜索