C# - How to replace question mark with any character

Discussion in 'ASP .Net' started by gogaz, Aug 24, 2004.

  1. gogaz

    gogaz Guest

    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
     
    gogaz, Aug 24, 2004
    #1
    1. Advertisements

  2. gogaz

    Karl Guest

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

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

    karl
     
    Karl, Aug 24, 2004
    #2
    1. Advertisements

  3. gogaz

    Shiva Guest

    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
     
    Shiva, Aug 24, 2004
    #3
  4. 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!
    ************************************************
     
    Cowboy \(Gregory A. Beamer\) [MVP], Aug 24, 2004
    #4
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.