Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / June 2007

Tip: Looking for answers? Try searching our database.

Removing escape sequences from strings

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JJ - 09 Jun 2007 14:45 GMT
Is there a way of checking that a line with escape sequences in it, has no
strings in it (apart from the escape sequences)?

i.e. a line with \n\t\t\t\t\t\t\t\r\n  would have no string in it
a line with \n\t\t\t\thello\t\t\n would hve the string 'hello' in it.

In others words, is there a method of removing all escape sequences from a
string?

I've tried Regex.Unescape(string) but this doesn't not seem to remove the
sequences.
Is there a way, or do I have to try and remove all the possible excape
sequences by parsing the string?

Thanks,
JJ
Mark Fitzpatrick - 09 Jun 2007 15:42 GMT
Have you tried just replacing them with nothing? If it's a string you could
do this like myString.Replace("\t","").Replace("\n","")

Signature

Hope this helps,
Mark Fitzpatrick
Former Microsoft FrontPage MVP 199?-2006

> Is there a way of checking that a line with escape sequences in it, has no
> strings in it (apart from the escape sequences)?
[quoted text clipped - 12 lines]
> Thanks,
> JJ
Mark Rae - 09 Jun 2007 15:46 GMT
> Is there a way of checking that a line with escape sequences in it, has no
> strings in it (apart from the escape sequences)?
[quoted text clipped - 9 lines]
> Is there a way, or do I have to try and remove all the possible excape
> sequences by parsing the string?

There may be a "cleverer" more efficient way of doing this, but how about:

string strEscaped = "\n\t\t\t\thello\t\t\n";
string strUnescaped = strUnescaped.Replace("\n",
"").Replace("\t","").Replace("\r","");

Signature

http://www.markrae.net

JJ - 09 Jun 2007 16:04 GMT
Thats what I ended up doing (string.replace). My worry was that there seems
to be a lot of these possible escape sequences beyond the standard \r \t \n
etc. so I foolishly thought that there might be a 'built-in' method that
removes all possible sequences.

Thanks again,
John

>> Is there a way of checking that a line with escape sequences in it, has
>> no strings in it (apart from the escape sequences)?
[quoted text clipped - 15 lines]
> string strUnescaped = strUnescaped.Replace("\n",
> "").Replace("\t","").Replace("\r","");
Mark Rae - 09 Jun 2007 16:56 GMT
> My worry was that there seems to be a lot of these possible escape
> sequences

In fact, there are only a handful:
http://msdn2.microsoft.com/en-us/library/4edbef7e(VS.80).aspx

Seems like a good candidate for a static method:

public static string StripEscape(string pstrText)
{
   string strStrippedText = pstrText;

   strStrippedText = strStrippedText.Replace("\t", String.Empty);
   strStrippedText = strStrippedText.Replace("\n", String.Empty);

   // etc

   return strStrippedText;
}

Obviously, the list of escape characters may change in future versions of
C#...

Signature

http://www.markrae.net


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.