C# Code Snippet - AddSlashes StripSlashes Escape String

C# Code Snippet - AddSlashes StripSlashes Escape String

C# Code Snippet - AddSlashes StripSlashes Escape String

(C-Sharp) C# code snippet AddSlahes tool allows quote a string with slashes (Escape String). Stripslashes snippet un-quotes a quote string by removing slashes.

Bookmark:

C# Code Snippet - AddSlashes StripSlashes Escape String

Addslashes snippet returns a string with backslashes before characters that need to be quoted in database queries etc. Special characters handle by these functions are: null, backspace, horizontal tab, new line, carriage return, substitute, double quote, single quote, backslash, and grave accent.

Despite the identical naming (addslashes, stripslashes), these functions are more comprehensive than their PHP equivalents. These functions have more features than PHP mysql_real_escape_string function by supporting backspace, horizontal tab etc This function must always (with few exceptions) be used to make data safe before sending a query to database (SQL Server, MySQL, Oracle ...etc.)

/// <summary>
/// Returns a string with backslashes before characters that need to be quoted
/// </summary>
/// <param name="InputTxt">Text string need to be escape with slashes</param>
public string AddSlashes(string InputTxt)
{
    // List of characters handled:
    // \000 null
    // \010 backspace
    // \011 horizontal tab
    // \012 new line
    // \015 carriage return
    // \032 substitute
    // \042 double quote
    // \047 single quote
    // \134 backslash
    // \140 grave accent

    string Result = InputTxt;

    try
    {
        Result = System.Text.RegularExpressions.Regex.Replace(InputTxt, @"[\000\010\011\012\015\032\042\047\134\140]", "\\$0");
    }
    catch (Exception Ex)
    {
        // handle any exception here
        Console.WriteLine(Ex.Message);
    }

    return Result;
}


/// <summary>
/// Un-quotes a quoted string
/// </summary>
/// <param name="InputTxt">Text string need to be escape with slashes</param>
public string StripSlashes(string InputTxt)
{
    // List of characters handled:
    // \000 null
    // \010 backspace
    // \011 horizontal tab
    // \012 new line
    // \015 carriage return
    // \032 substitute
    // \042 double quote
    // \047 single quote
    // \134 backslash
    // \140 grave accent

    string Result = InputTxt;

    try
    {
        Result = System.Text.RegularExpressions.Regex.Replace(InputTxt, @"(\\)([\000\010\011\012\015\032\042\047\134\140])", "$2");
    }
    catch (Exception Ex)
    {
        // handle any exception here
        Console.WriteLine(Ex.Message);
    }

    return Result;
}


C# Keywords Used:

  • RegularExpressions
  • Regex
  • Replace

Code Snippet Information:

  • Applies To: .Net, C#, AddslaShes, StripSlashes, Quote string with slashes, Un-quotes a quoted string, Escaped string, Un-Escaped string, Escapes special characters in a string for use in a SQL statement
  • Programming Language : C#

External Resources:

Botnari :: July 19-2010 :: 09:35 AM

Thanks,
useful article, helped me a lot.

Leave a comment