C# - How to replace question mark with any character

  • Thread starter Thread starter gogaz
  • Start date Start date
G

gogaz

Hi there,
I have a small problem. I need to replace the "?" character from user
input string to "-". I have tried:

Regex.Replace(sStr,"\?","-"); //GIVES ERROR
Regex.Replace(sStr,@"?","-"); //GIVES ERROR
Regex.Replace(sStr,@"\?","-"); //DOES NOT WORKS AS IT LOOKS FOR "\?"

Please help!

Regards
 
str = str.Replace("?" "-");

If there's a particular reason you need to use a regular expression, let us
know.

karl
 
How about String.Replace() ?

Hi there,
I have a small problem. I need to replace the "?" character from user
input string to "-". I have tried:

Regex.Replace(sStr,"\?","-"); //GIVES ERROR
Regex.Replace(sStr,@"?","-"); //GIVES ERROR
Regex.Replace(sStr,@"\?","-"); //DOES NOT WORKS AS IT LOOKS FOR "\?"

Please help!

Regards
 
I agree with Karl on this one. The issue here is the ? has a special meaning
in Regex. To do what you desire, I believe you would have to use
Regex.Escape to escape the ? (make it a ? instead of the Regex special
character ?).

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top