Truemag

  • Categories
    • Tips And Tricks
    • Internet
    • PHP
    • Javascript
    • CSharp
    • SQL Server
    • Linux
  • Lastest Videos
  • Our Demos
  • About
  • Contact
  • Home
  • Write With Us
  • Job Request
Home CSharp C# ASP.NET Remove leading and trailing quotes(‘) or double quotes (“)

C# ASP.NET Remove leading and trailing quotes(‘) or double quotes (“)

I have a SQL Server table that I have imported from a Text file. In this process, a bunch of the entries have quote marks leading and trailing the entry of several data rows. For example the the table ‘tblBlog’ with colunms : Site, Owner, Partner.
The sample row of text file like below corresponds with the columns of tblBlog :
‘4rapiddev.com’ ‘[email protected]’ ‘[email protected]’

Now, before importing to database, I need to remove leading and trailing quotes(‘) of these fields.
There are many way to solve, for example you can use C# function SPLIT and REPLACE
In this article, I would like to introduce to you the way of removing leading and trailing quotes(‘) or double quotes(“) by using C# Regular Expression of namespace System.Text.RegularExpressions

1. Default.aspx – Design HTML layout

A text box to input data, a button to remove removing leading and trailing quotes(‘) and a button to remove removing leading and trailing double quotes(“). Please see the image at the bottom of this article for more detail.

<form id="form1" runat="server">
<div>
 <strong>Input Data : </strong><br />
 <asp:TextBox ID="txtData" runat="server" text="" TextMode="MultiLine" Rows="5" Width="400px">
 </asp:TextBox>
 <br />
 <asp:Button ID="btnRemoveQuote" runat="server" Text="Remove Quotes" 
        onclick="btnRemoveQuote_Click"/>
        <asp:Button ID="btnRemoveDoubleQuote" runat="server" Text="Remove Double Quotes" 
        onclick="btnRemoveQuote_Click"/>
  <br /><br /> <strong>Data for testing :</strong>
  <br />
 <asp:Label ID="lblQuotes" Text="'4rapiddev.com' '[email protected]' '[email protected]'" runat="server"></asp:Label>
 <br />
 <asp:Label ID="lblDoubleQuotes" Text='"4rapiddev.com" "[email protected]" "[email protected]"' runat="server"></asp:Label>
</div>
</form>

<form id="form1" runat="server"> <div> <strong>Input Data : </strong><br /> <asp:TextBox ID="txtData" runat="server" text="" TextMode="MultiLine" Rows="5" Width="400px"> </asp:TextBox> <br /> <asp:Button ID="btnRemoveQuote" runat="server" Text="Remove Quotes" onclick="btnRemoveQuote_Click"/> <asp:Button ID="btnRemoveDoubleQuote" runat="server" Text="Remove Double Quotes" onclick="btnRemoveQuote_Click"/> <br /><br /> <strong>Data for testing :</strong> <br /> <asp:Label ID="lblQuotes" Text="'4rapiddev.com' '[email protected]' '[email protected]'" runat="server"></asp:Label> <br /> <asp:Label ID="lblDoubleQuotes" Text='"4rapiddev.com" "[email protected]" "[email protected]"' runat="server"></asp:Label> </div> </form>

2. Default.aspx.cs – Create code behind to process pressing button “Remove Quotes” and “Remove Doubles Code”

The main point of this block code is to declare function GetData based on two parameters : input data and pattern. We use Regular Expression to match input data with input pattern and return list of corresponding strings.
++ Pattern “‘(.*?)'” : match with data between quotes(‘).
++ Pattern \”(.*?)\” : match with data between doublquotes(“).

using System.Text.RegularExpressions;
 
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.txtData.Text = this.lblQuotes.Text;
    }           
}
protected void btnRemoveQuote_Click(object sender, EventArgs e)
{
    Button button =  sender as Button;
    List<string> lstValue = new List<string>();
    if (this.btnRemoveQuote.Text.Equals(button.Text)) // Process pressing button "Remove Quotes"
    {
        lstValue = GetData(this.txtData.Text, "'(.*?)'");
    }
    else if (this.btnRemoveDoubleQuote.Text.Equals(button.Text))  // Process pressing button "Remove Double Quotes"
    {
        lstValue = GetData(this.txtData.Text, "\"(.*?)\"");
    }
 
    // Display result
    Response.Write("<strong>Result:</strong><br/>");
    Response.Write("======================================================<br/>");
    if (lstValue.Count == 0)
        Response.Write("<strong>No data found!</strong><br/>");
    for (int i = 0; i < lstValue.Count; i++)
    {
        Response.Write(string.Format("{0} : {1}<br/>", i + 1, lstValue[i]));
    }
    Response.Write("======================================================<br/>");
}
 
/// <summary>
/// Get list of strings of input data based on input pattern.
/// </summary>
/// <param name="text">Input data</param>
/// <param name="pattern">quote patter :"'(.*?)'" or double pattern : "\"(.*?)\"</param>
/// <returns>List of strings</returns>
List<string> GetData(string text, string pattern)
{
    List<string> retList = new List<string>();
    Regex rex = new Regex(pattern);
    MatchCollection mc = rex.Matches(text);
    foreach (Match m in mc)
    {
        if (m.Success)
        {
            retList.Add(m.Groups[1].ToString());
        }
    }
    return retList;
}

using System.Text.RegularExpressions; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.txtData.Text = this.lblQuotes.Text; } } protected void btnRemoveQuote_Click(object sender, EventArgs e) { Button button = sender as Button; List<string> lstValue = new List<string>(); if (this.btnRemoveQuote.Text.Equals(button.Text)) // Process pressing button "Remove Quotes" { lstValue = GetData(this.txtData.Text, "'(.*?)'"); } else if (this.btnRemoveDoubleQuote.Text.Equals(button.Text)) // Process pressing button "Remove Double Quotes" { lstValue = GetData(this.txtData.Text, "\"(.*?)\""); } // Display result Response.Write("<strong>Result:</strong><br/>"); Response.Write("======================================================<br/>"); if (lstValue.Count == 0) Response.Write("<strong>No data found!</strong><br/>"); for (int i = 0; i < lstValue.Count; i++) { Response.Write(string.Format("{0} : {1}<br/>", i + 1, lstValue[i])); } Response.Write("======================================================<br/>"); } /// <summary> /// Get list of strings of input data based on input pattern. /// </summary> /// <param name="text">Input data</param> /// <param name="pattern">quote patter :"'(.*?)'" or double pattern : "\"(.*?)\"</param> /// <returns>List of strings</returns> List<string> GetData(string text, string pattern) { List<string> retList = new List<string>(); Regex rex = new Regex(pattern); MatchCollection mc = rex.Matches(text); foreach (Match m in mc) { if (m.Success) { retList.Add(m.Groups[1].ToString()); } } return retList; }

3. Demo & Download source code

Demo - Remove quotes with Regular expression

Demo - Remove quotes with Regular expression

Mar 18, 2012quynhnguyen

Source Code Demo page

Manage MacBook Hosts File Easier With Hosts WidgetExample Of crossdomain.xml - Cross Domain Policy File
You Might Also Like:
  • ASP.NET JQuery Autocomplete Textbox
  • Javascript LTrim,RTrim,Trim function
  • JQuery Dialog Hide Or Remove Close Button
  • MSSQL Trim Function
  • Linux remove entire directory
  • PHP remove vietnamese accents
  • PHP Remove All Special Characters And Replace Spaces With Hyphens
  • ASP.NET Get Facebook URL Total Comments, Total Likes, Total Shares
  • Function Remove All HTML Tags In PHP
  • ASP.NET Input only digits in TextBox using Ajax
quynhnguyen
Link9 years ago CSharpASP.NET, regular expression, split941
0
GooglePlus
0
Facebook
0
Twitter
0
Digg
0
Delicious
0
Stumbleupon
0
Linkedin
0
Pinterest
Most Viewed
PHP Download Image Or File From URL
22,198 views
Notepad Plus Plus Compare Plugin
How To Install Compare Text Plugin In Notepad Plus Plus
20,070 views
Microsoft SQL Server 2008 Attach Remove Log
Delete, Shrink, Eliminate Transaction Log .LDF File
15,848 views
JQuery Allow only numeric characters or only alphabet characters in textbox
13,327 views
C# Read Json From URL And Parse/Deserialize Json
9,819 views
4 Rapid Development is a central page that is targeted at newbie and professional programmers, database administrators, system admin, web masters and bloggers.
Recent Posts
  • Free Online Photo Editor
  • Easy Tips For Writing An Essay
  • What Can I Expect From An Academic Essay Service?

  • Can Be Essay Service Companies Good For You?

  • Tips To Choosing The Ideal Essay Writers
Categories
  • CSharp (45)
  • Facebook Graph API (19)
  • Google API (7)
  • Internet (87)
  • iPhone XCode (8)
  • Javascript (35)
  • Linux (28)
  • MySQL (16)
  • PHP (84)
  • Problem Issue Error (29)
  • Resources (32)
  • SQL Server (25)
  • Timeline (5)
  • Tips And Tricks (141)
  • Uncategorized (112)
Recommended
  • Custom Software Development Company
  • Online Useful Tools
  • Premium Themes
  • VPS
2014 © 4 Rapid Development