Re: replace

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Bruce Wood (brucewood_at_canada.com)
Date: 02/02/05


Date: 2 Feb 2005 09:51:10 -0800

Ahhh... so the string you have was read from a file, not compiled into
your program. That makes all of the difference in the world. If the
string is read from a file, it really does have backslashes in it. If
it was compiled into your program, it doesn't.

Evidently, Excel processes the string during input and converts the
escape sequences (\", \t, \n, and the like) into their corresponding
characters (", tab, and newline, respectively).

I don't know of any built-in .NET method that will this processing for
you (take a string containing escape sequences and translate them to
their character equivalents). To do a 100% job would be a huge
challenge, but you can do the most common sequences (and perhaps all of
the ones you need) like this:

string cleanString;
cleanString = stringFromXmlFile.Replace("\\\"", "\"").Replace("\\t",
"\t").Replace("\\n", "\n");

You can add as many "replace" calls as you need to do the job.

The first Replace call is different because it involves the
double-quote, which is also the string delimeter in C#, so it needs
some special help. This replace call says, "Replace every occurrence of
a backslash (\\, because a \ by itself is an escape, so two \\
represent a single \ to the compiler) followed by a double-quote (\"
because you can't just say " or that would end the string) to a
double-quote."

The subsequent Replace calls are simpler. They say, "Replace every
occurrence of a backslash (\\ again) followed by (for example) an en by
(for example) the character \n." The compiler translates \n into the
newline character, so the Replace ends up replacing every two-character
sequence \n by the new line character.

You can do the same for tab (\t), return (\r), and a bunch of other
characters. See here for the common ones:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_2_4_4_4.asp

(watch for line wrapping on the URL).

Mind you, this won't trap all possible values: you can also use the \
escape character in C# followed by a hexadecimal or Unicode escape
sequence, which could indicate any character in the massive Unicode
character space. This Replace trick won't work in that case. I'll leave
it to someone smarter than I am to come up with a solution for that
one.

However, if you don't have any backslashes followed by numeric
sequences in your XML code, then you're probably fine with the trick I
outlined above.



Relevant Pages

  • Re: PL/I string representations
    ... >> of the language, so it was interesting to me, hopefully it will be to ... I found a workable compiler for Fortran in 1971 (with a bug ... >> The specified length is the minimum, and each time a character ... >> string is assigned to E, the length is stored with it. ...
    (comp.programming)
  • Re: To "TAB" or not to "TAB"
    ... > negative I want etc. in character strings now in every compiler I'm ... If there is a graphic character in the string, ... that you are saying that the compiler should do no special processing, ...
    (comp.lang.fortran)
  • Re: when substring equals string
    ... CHARACTER:: String ... The unit number is attached to a file via an OPEN statement. ... question is about a character variable, ... Like in the way the operator invoked the compiler. ...
    (comp.lang.fortran)
  • Re: Newbie: Is it possible for a function to return a String or Character Arrays?
    ... The hard part about this is not returning a string, ... If you wanted a fixed-length string returned, you could declare the ... If your f95 compiler implements the allocatable stuff TR (or if you ... I don't think that the allocatable TR included allocatable character ...
    (comp.lang.fortran)
  • Re: Using RegEx
    ... itself be escaped to ensure the string is compiled correctly. ... because there are two systems using the same type of escape sequences. ... that must get to RegEx past the compiler need to be included in the ...
    (comp.sys.acorn.programmer)