replace

  • Thread starter Thread starter Etu
  • Start date Start date
E

Etu

Hi,

I have a string:

string c = "'abc' \"cde\", 'mno' \"xyz\",";

how can I use the c.Replace(???, ???) method to have this string:

"'abc' "cde", 'mno' "xyz","
that is, all the backslashes are removed.

Thanks,

Etu
 
In the line of code you wrote:

string c = "'abc' \"cde\", 'mno' \"xyz\",";

there are no backslashes in the string. The backslashes you see are
only a typographical convention that tells the compiler that the
double-quote that follows each backslash is not the double-quote that
ends the string, but that it should instead place a double-quote
character in the string.

The string stored internally after compilation looks like this:

'abc' "cde", 'mno' "xyz",

What are you trying to do in your program and what problems are you
encountering, since backslashes in the string don't appear to be what's
wrong?
 
string c = "'abc' \"cde\", 'mno' \"xyz\",";
string clearString = c.Replace('\\', ' '); // You must type backslash twice

That's All

Hopes This Helps
 
Thanks Bruce!

The problem I have is actually my program first reads some strings
(with \t, \" and so on) from an XML file and writes these strings to an
Excel file. After the Excel file is saved, \t becomes a space and \"
becomes ". Then when my program later reads the strings from the Excel
file, it doesn't think the strings are the same as those in the XML
file.

That is why I was trying to modify the strings from the XML file by
getting rid of the backslashes - try to do the same thing that Excel
does, but could not find a way to do so.

Thanks
 
SerhaD Inanli said:
string c = "'abc' \"cde\", 'mno' \"xyz\",";
string clearString = c.Replace('\\', ' '); // You must type backslash
twice

Yes but will this make any difference... to the out put... c and clearString
both are same as you out them...???
 
private void RegEx_Click(object sender, System.EventArgs e)

{

string c = "'abc' \"cde\", 'mno' \"xyz\",";

try

{

textBox1.Text = c.Replace('\\', ' '); // Second parameter is a blank char

}

catch(Exception ex)

{


MessageBox.Show(ex.Message);

}

}

and Result is = 'abc' "cde", 'mno' "xyz",

I Tried several times , do u get different output or Error ?
 
Etu,

Why do you think that your C# program doesn't think the strings are the same
as those in the XML file?

If you are watching this in the debugger then you will see tabs replaced
with \t, quotes replaced with \" but this is just a display convention. The
debugger wants to display all strings like C# constants, and a C# constant
can't contain a quote, so it'll be changed to \" to be syntactially correct.
And any escapable characters will be transformed to escape sequences so that
you can see they are there.

If you are reading the XML files and they don't have the backslashes then C#
won't magically add them back in.

In a similar vein, why do you think that Excel changes tabs to spaces? It
might be doing so, particularly if it doesn't understand \t but realizes
it's some kind of escape sequence and changes it to a space; or, it may know
it's a tab but disallows or transforms them in that context. But if you are
going by what your eye sees to draw this conclusion then you may be drawing
the wrong conclusion. It may well *be* a tab.

--Bob
 
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/d...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.
 
I appreciate your help, Bob. I jumped to the wrong conclusion but
later found that the reason my program doesn't "think" the two strings
are the same because the first apostrophe was not saved to the Excel
file!

Etu
 
Back
Top