Convert ‘\\’ to ‘\’ in c#

This post would sound pretty weird to any c# developer. However, a situation i had been in last week forced me to bring this into notice.

I had an XML file in which i had node values like ‘Requirements Management\XYZ’. I read the XML using XLinq and the c# string looked like ‘Requirements Management\\XYZ’. At first i felt its fine, as C# compiler added that extra backslash to handle an escape sequence. But when i tried using this value (stored in a variable), it was always returning me the string with ‘\\’. 😛

After 4 hours of trial and error, i found the solution. Believe it or not, i tried the extreme of exploiting the ‘Split’ and ‘Replace’ methods string gives me. 😛


Whenever you need to assign this variable to some object property, in my case Area Path and Iteration Path for my TFS 2010 SDK API, you just need to make use of the powerful ‘@’ operator & String.Format method

string strXyz = Xdoc..Load(“myXML.xml”).Element(“abc”).Value; //This returned ‘Requirements Management\\XYZ’

Now to use this:

Obj.XYZ = String.Format(@”{0}”,strXyz); //The string value assigned to my ‘Obj’ object will be ‘Requirements Management\XYZ’.

It doesn’t make much sense, but this will handle the conversion of the double slashes to slash conversion.

But if you are using strings all the way, there’s nothing to worry, C# compiler will handle such situations. For instance displaying it on a textbox or some label or something.

Ideally you cannot convert double slashes to single slashes.. 🙂

How to get the javascript version?

Javascript is evolving. With newer version, more commands, new functionalities gets added. To get to know the version of javascript your browser supports, just save the following code as html and load it in your browser:

<script type=”text/javascript”>
var jsver = 1.0;
<script language=”Javascript1.1″>
jsver = 1.1;
<script language=”Javascript1.2″>
jsver = 1.2;
<script language=”Javascript1.3″>
jsver = 1.3;
<script language=”Javascript1.4″>
jsver = 1.4;
<script language=”Javascript1.5″>
jsver = 1.5;
<script language=”Javascript1.6″>
jsver = 1.6;
<script type=”text/javascript”>
document.write(‘<p><b>Javascript version ‘ + jsver
+ ‘ supported<\/b><\/p>’);

Most of the times, you can expect the following results:

Javascript version 1.6 supported

Browser and javascript version supported are as below:

Firefox, Netscape 7/8, Mozilla, and Opera 7+ all support version 1.6

Netscape 6 supports version 1.5

Opera 6 supports version 1.4

Netscape 4.5, Opera 5, and Internet Explorer 6 support version 1.3

Netscape 4 supports version 1.2