【C#】去除字符串中的特殊字符

學習了一下Trim的用法,挺實用的。

前臺代碼:

<html xmlns="http://www.w3.org/1999/xhtml"> 

<head runat="server"> 

<title>Trimi小技巧</title> 

</head> 

<body>

<form id="form1" runat="server">

<div>

<asp:TextBox ID="txtTrim" runat="server"></asp:TextBox> 

<asp:Button ID="btnTrim" runat="server" Text="確定" OnClick="btnTrim_Click" />

</div>

</form>

</body>

</html>

後臺代碼:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Trim : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnTrim_Click(object sender, EventArgs e)    
    {
        char[] TrimChar = { ' ', '-', '\'', '\"', '\\' };       //此處使用了轉義字符如:\',\",\\,分別表示單引號,雙引號,反斜槓
        txtTrim.Text = this.txtTrim.Text.Trim(TrimChar);        
    }    
}

挺好用的,轉載自:https://www.cnblogs.com/shanaihuan/archive/2011/04/13/2014565.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章