Welcome to AddressOf.com Sign in | Help

VB.NET string method to handle escape sequences.

After reading some comments by Alex Hoffman, I've put together a quick and dirty function that will do some of what he is complaining that VB.NET isn't capable of doing.  I've also made it so that this particular function doesn't require a reference to the VisualBasic namespace since he was complaining about that too.  I like my VisualBasic namespace... it's my friend :-)

Here's the quick and dirty escape sequence string function to assist converting code from C# to VB.NET.

Function EscapeString(ByVal s As String) As String
  '\' - Single quote 0x0027
 
If s.IndexOf("\'") > 0 Then
   
s = s.Replace("\'", "'")
 
End If
 
'\" - Double quote 0x0022
 
If s.IndexOf("\" & Chr(&H22)) Then
   
s = s.Replace("\" & Chr(&H22), Chr(&H22))
  End If
  '\0 - Null 0x0000
  If s.IndexOf("\0") Then
    s = s.Replace("\0", Chr(&H0))
  End If
  '\a - Alert 0x0007
  If s.IndexOf("\a") > 0 Then
    s = s.Replace("\a", Chr(&H7))
  End If
  '\b - Backspace 0x0008
  If s.IndexOf("\b") > 0 Then
    s = s.Replace("\b", Chr(&H8))
  End If
  '\f - Form feed 0x000C
  If s.IndexOf("\f") > 0 Then
    s = s.Replace("\f", Chr(&HC))
  End If
  '\n - New line 0x000A
  If s.IndexOf("\n") > 0 Then
    s = s.Replace("\n", Environment.NewLine)
    'or s = s.Replace("\n", vbLf)
    'or s = s.Replace("\n", Chr(10))
    'or s = s.Replace("\n", Chr(&HA))
  End If
  '\r - Carriage return 0x000D
  If s.IndexOf("\r") > 0 Then
    s = s.Replace("\r", Chr(&HD)
    'or s = s.Replace("\n", Chr(13))
    'or s = s.Replace("\n", vbCR)
  End If
  '\t - Horizontal tab 0x0009
  If s.IndexOf("\v") Then
    s = s.Replace("\v", Chr(&H9))
  End If
  '\v - Vertical tab 0x000B
  If s.IndexOf("\v") Then
    s = s.Replace("\v", Chr(&HB))
  End If
  '\\ - Backslash 0x005C
  If s.IndexOf("\\") Then
    s = s.Replace("\\", Chr(&H5C))
  End If
  Return s
End Function

I'm Checking the IndexOf so that we don't create any unncessary string copies during the replacement process.  This helps a bit on the performance and memory manager.

Again, this is quick and dirty; so there could be some issue under some specific escape character combinations.  Anyone have a better suggestion for doing this?  First though that comes to my mind would be possibly using regular expressions.

Published Sunday, April 27, 2003 2:54 PM by CorySmith

Comments

# re: VB.NET string method to handle escape sequences.

Thursday, June 10, 2004 11:54 AM by anonymous
I assume for horizontal tab you mean "\t"?

# environment.newline

Thursday, June 17, 2004 7:31 AM by anon
i used to use chr(13) for a new line but that doesn't work in vb .net
thanks for the info

# re: VB.NET string method to handle escape sequences.

Thursday, July 22, 2004 3:31 AM by manpreet singh bhatia
i want white paper on vb.net

# re: VB.NET string method to handle escape sequences.

Sunday, November 21, 2004 6:27 PM by Brett
Use the shared System.Text.RegularExpressions.Regex.Unescape method instead!

# re: VB.NET string method to handle escape sequences.

Tuesday, January 18, 2005 5:01 AM by Andrew
How to detect the beginning of the carriage return and line feed from text file using VB.NET? Pls Help

# re: VB.NET string method to handle escape sequences.

Tuesday, January 18, 2005 1:35 PM by Cory Smith
If your familiar with how to do it with VB6-, you could use the same method. However, with the latest versions of VB, you could use something like the following:

crlfIndex = textFileAsString.IndexOf(vbCrLf)

This would give you the index offset of the first occurance of the cr lf combination. IndexOf also allows you to specify a starting offset to look from.

If your text file is relatively small, just load the file into a string variable. If it's large, then you will have to use either a seeking method of the Stream class and look at one byte at a time (where byte would equal 13). If you run into a 13, check if the next byte is a 10. If it's true, then you found a CRLF combination in the stream. Instead of doing one byte at a time though, I would recommend reading a chunk of the file into memory (a buffer) and evaluate that way. Reading one byte at a time in a large file could take a while.

# re: VB.NET string method to handle escape sequences.

Friday, May 13, 2005 8:54 AM by Roni
Hi, Can I use this with select statement, where the dataset is assigned to a Datagrid throws an error because of single quote in the field value. Please reply me to ronimathews@hotmail.com

Thank you very much
Roni from UK

# re: Answer to Roni

Wednesday, May 25, 2005 5:01 AM by Daniel Goodhew
Roni, I had the same problem as you. I assume the input is coming from a textbox?? For instance:
Textbox1 has the text: "Don't"
so using the sub below: Fixtext(textbox1)

textbox1.text is now "Don''t" and when this is inputted into a database the resulting text is "Don't".

Feel free to use the code for what ever, just remember if any of your friends are ever building a house tell them to go to: www.betterchoicehib.com.au

Private Sub FixText(ByVal BadText As Control)
Dim CharCount As Integer
Dim CurrentChar As String
For CharCount = 1 To Len(BadText.Text)
CurrentChar = (Mid(BadText.Text, _
CharCount, 1))
If CurrentChar = "'" Then
BadText.Text = Mid _(BadText.Text, 1, CharCount) & "'" & Mid _(BadText.Text, CharCount + 1)
CharCount += 1
End If
Next
End Sub

# re: VB.NET string method to handle escape sequences.

Tuesday, August 16, 2005 4:41 PM by anonymous
Its like you just farted this code out after a hard night of drinking!

# re: VB.NET string method to handle escape sequences.

Saturday, November 05, 2005 8:59 AM by rajesh
its ok

i want with example

# re: VB.NET string method to handle escape sequences.

Monday, November 21, 2005 10:38 PM by Daniel Goodhew
How did you know that I farted?? was pretty crap wasn't it.

Should have just been

'Replace single "'" with double"''"
'Allow Access data entry
Private Function FixText(CheckThis as string) as string
return(replace(checkthis,"'","''"))
End Sub

# re: VB.NET string method to handle escape sequences.

Wednesday, December 14, 2005 1:48 PM by Phil McCrackin
The colors the colors get these dame ants off me!.

# re: VB.NET string method to handle escape sequences.

Thursday, May 18, 2006 2:33 AM by Dnyaeshwar
Thanx,
I came to know some new thing, specially how to add a new line in teztbox

# re: VB.NET string method to handle escape sequences.

Monday, May 22, 2006 7:04 PM by anon
Why in the world did you not just use one loop to increment through the string and put each character through a Select? With that many IndexOf's, you can't expect this to be efficient.

Old-school C programmers ftw

# re: VB.NET string method to handle escape sequences.

Wednesday, August 30, 2006 12:01 PM by Girish Keskar
Wow,
It helped me a lot.
These couple of lines are really fantastic.
I was exactly looking for the same.

Thanx.

# re: VB.NET string method to handle escape sequences.

Wednesday, November 15, 2006 11:11 AM by Matthew Bates, CSULB

Instead of using the VB6 "vbCrLf", consider using the built-in VB.Net ControlChars (focusing on forward instead of backward compatibility).  We have access to: ControlChars.Back, ControlChars.Cr, ControlChars.CrLf, ControlChars.FormFeed, ControlChars.Lf, ControlChars.NewLine, ControlChars.NullChar, ControlChars.Quote, ControlChars.Tab, and ControlChars.VerticalTab, though for quotes, using two double quotes is easier (e.g. "She said, ""Hi,"" today.")

Anonymous comments are disabled