Visual Basic Code Snippet - AddSlashes StripSlashes Escape String
(VB) Visual Basic code snippet AddSlahes tool allows quote a string with slashes (Escape String). Stripslashes snippet un-quotes a quote string by removing slashes.
Bookmark:
Visual Basic 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> Public Function AddSlashes(ByVal InputTxt As String) As String ' 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 Dim Result As String = InputTxt Try Result = System.Text.RegularExpressions.Regex.Replace(InputTxt, "[\000\010\011\012\015\032\042\047\134\140]", "\$0") Catch Ex As Exception ' handle any exception here Console.WriteLine(Ex.Message) End Try Return Result End Function
''' <summary> ''' Un-quotes a quoted string ''' </summary> ''' <param name="InputTxt">Text string need to be escape with slashes</param> Public Function StripSlashes(ByVal InputTxt As String) As String ' 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 Dim Result As String = InputTxt Try Result = System.Text.RegularExpressions.Regex.Replace(InputTxt, "(\\)([\000\010\011\012\015\032\042\047\134\140])", "$2") Catch Ex As Exception ' handle any exception here Console.WriteLine(Ex.Message) End Try Return Result End Function
VB Keywords Used:
- RegularExpressions
- Regex
- Replace
Code Snippet Information:
- Applies To: .Net, VB, Visual Basic, 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 : Visual Basic (VB)
External Resources: