C++/CLI Code Snippet - AddSlashes StripSlashes Escape String

C++/CLI Code Snippet - AddSlashes StripSlashes Escape String

C++/CLI Code Snippet - AddSlashes StripSlashes Escape String

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

Bookmark:

C++/CLI 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>
/// Quote string with slashes
/// </summary>
/// <param name="InputTxt">Text string need to be escape with slashes</param>
System::String ^AddSlashes(System::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

    System::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>
System::String ^StripSlashes(System::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

    System::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++/CLI Keywords Used:

  • RegularExpressions
  • Regex
  • Replace

Code Snippet Information:

  • Applies To: .Net, C++/CLI, CLI, 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++/CLI

External Resources:

Leave a comment