Share via


Replace Escape char in string

Question

Friday, November 26, 2010 10:22 AM

Hello all,

I have a following string

"<Grid><IO/>\n<Body>\n<B>\n<I id=\AR1\ Def=\R\ Panel=\\ Id=\1\ Name=\Activity 1\ Prent=\\ ResourceName=\Emp 1\ S=\11/26/2010 12:00:00 AM\ E=\12/16/2010 12:00:00 AM\ Predecessor=\0\/></B>\n</Body>\n</Grid>"

And i want it in following form

<Body>

    <B>

      <I id="AR1" Def="R" Panel="" Id="1" Name="Activity 1" Prent="" ResourceName="Emp 1" S="11/26/2010 12:00:00 AM" E="12/16/2010 12:00:00 AM" Predecessor="0"/>     

    </B>

  </Body>

</Grid>

 

It means i want to replace \ char from upper string. How can i do this. please help.

I am struck very badly. Thanks in advance. please help.

All replies (5)

Wednesday, December 1, 2010 12:27 AM âś…Answered

Got a solution..... :)

StringBuilder sb1 = new StringBuilder();
                sb1.AppendLine(Your string with escap char);
                sb1.Replace('\n', ' ');


                string str = sb1.ToString();
                sb1.Replace(Convert.ToChar(34), '"');


                str = sb1.ToString();
                sb1.Replace('\r', '"');

                string tempStr = "";
                for (int i = 0; i < sb1.Length; i++)
                    if (sb1[i] == 34)
                        tempStr += "'";
                    else
                        tempStr += sb1[i];

Enjoy.....................

 

 


Friday, November 26, 2010 10:47 AM

It means i want to replace \ char from upper string. How can i do this. please help.

 

This work ..?

string s = "\n<B>\n<I id=\"AR1\" Def=\"R\" 1\" Name" ; //your string
string test = s.Replace(@"\", string.Empty); 

 


Saturday, November 27, 2010 4:34 AM

No it does not work... any other suggestions please..... 


Saturday, November 27, 2010 5:39 PM

Your problem is that the input string does not have the \ char in it.  It has some non-printable characters which are made visible for various debugging purposes with a pair of characters.  The first of the pair is the \  I can see that you have the newline character which is represented as \n.  Similarly you have a double quote character which is represented as \

I am guessing that you are trying to display this string on a web page.  In that case you will want to replace the newline character \n with <BR/> to get a roughly similar visual effect.

inStr.Replace("\n", "<BR/>");

You should read chapter 5 in the book mentioned in my signature.


Wednesday, December 1, 2010 12:40 AM

Wow!