C# - How to replace question mark with any character

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
 
K

Karl

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

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

karl
 
S

Shiva

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
 
C

Cowboy \(Gregory A. Beamer\) [MVP]

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

Staff online

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top