tags:

views:

64

answers:

3

Hi,

Am using regex.Replace() to replace the whole occurrence of a string.. so I gave like Regex.Replace(str,@stringToReplace,"**"); where stringToReplace = @"session" + "\b";

if i give like that its not replacing.. but if i give like Regex.Replace(str,@"session\b","**"); then its working.. how to avoid this.. i want to pass value which will be set dynamically..

Thanks nimmi

+4  A: 

try

stringToReplace = @"session" + @"\b";
Sam Holder
+1 for fastest.
Mark Byers
Its working. Great.. Thanku so much..
Nimmi
@Nimmi: You should (if possible) choose the most helpful/correct answer and select the checkmark next to it to mark it as "accepted". Also upvote good answers (I just upvoted your question, now you have enough reputation to do so).
Tim Pietzcker
A: 

@"session" + "\b" and @"session\b"

are not the same string. In the first case "\b" you don't treat the slash as a slash but as an escape parameter. In the second case you do.

So @"session" + @"\b" should bring the same result

Gerald Keider
+4  A: 

The @ here means a verbatim string literal.

When you write "\b" without the @ it means the backspace character, i.e. the character with ASCII code 8. You want the string consisting of a backslash followed by a b, which means a word boundary when in a regular expression.

To get this you need to either escape the backslash to make it a literal backslash: "\\b" or make the second string also into a verbatim string literal: @"\b". Note also that the @ in @"session" (without the \b) doesn't actually have an effect, although there is no harm in leaving it there.

stringToReplace = "session" + @"\b";
Mark Byers
+1 for the most complete explanation. :)
Sam Holder