I have a string that reads "hello.world", and what I would like to do is to change this to "hello\.world" so that I can use it in a regular expression and have the period used as a period - rather than as a wildcard (which it is in a regular expression)
The problem occurs when I try to add a single backslash
Example Code:
string s = "hello.world";
char ch = (char)0x5C;
s += ch;
result:
s = "hello.world\\";
---------
string s = "hello.world";
s = s.Replace(".", @"\.");
result:
s = "hello\\.world";
---------------
string s = "hello.world";
s += @"\";
result:
s = "hello.world\\";
When I go to display the string in a messagebox - it displays fine because the double backslash is used as an escape character - however, when trying to use the string for a regular expression, or any kind of string comparison - it's useless.
Note: I have tested, and this only occurs in Visual Studio 2005, not in 2003.
So my question is: How can I add a single backslash to a string in Visual Studio 2005
Thanks
El Carrot

How do I Add Single Back Slashes to a String in Visual Studio 2005
Hedda
FROM MSDN
The regular expression
\s2000, when applied to a body of text, matches all occurrences of the string "2000" that are preceded by any white-space character, such as a space or a tab.If you are using C++, C#, or JScript, special escape characters, such as \s, must be preceded by an additional backslash (for example, "
\\s2000") to signal that the backslash in the escape character is a literal character. Otherwise, the regular expression engine treats the backslash and the s in\sas two separate operators. You do not have to add the backslash if you are using Visual Basic 2005. If you are using C#, you can use C# literal strings, which are prefixed with @ and disable escaping (for example,@"\s2000").CMAN5000
you are confused because of debugger of VS 2005.
your strings are fine - just debugger adds additional slash as it is normal in C oriented languages.
it is better to use this way
hope this helps
dannymking
Thanks for the response. It appears that you are correct - it's just quite confusing when I'm tracking it through the debugger and I see a string value which doesn't match what I have set.
The other part that makes it confusing is that Visual Studio 2003 doesn't do this.
LOURENÇO MANTOVANI