none
Replace a character in a string C#

    Question

  • Hi. I can`t figure how to replace string in C#.

     

    string s = "cat\temp\sounds\az_A001_l'ai_7a840_.wav";
    string pattern = "\\([^\\]*).wav";
    string s2 = s.Replace(pattern, "\BIN\$1.bin");
    

     

    This code works in demo witch i get from http://www.regular-expressions.info/

     s2 has to be equal after replacement ""cat\temp\sounds\BIN\az_A001_l'ai_7a840_.bin";  "

    But in visual studio doesn't work :(. I am programing with Visual studio.

    P.S. Sorry for English if I did mistakes...

    Please help.

    • Moved by pagerwho Sunday, November 07, 2010 5:17 PM Moved to proper forum for question (From:The Official Scripting Guys Forum!)
    Sunday, November 07, 2010 4:59 PM

Answers

  • Hello,

     

    Try something like this:

          string s = @"cat\temp\sounds\az_A001_l'ai_7a840_.wav";
          string pattern = @"\\([^\\]*).wav";
          string s2 = Regex.Replace(s, pattern, @"\BIN\$1.bin");
    Hope this helps, if you have any other questions or comments, please let me know,


    Best Regards,
    Emanuel Varga

     


    If a post answers your question, please click "Mark As Answer " on that post and "Mark as Helpful ".
    • Edited by Emanuel, Varga Sunday, November 07, 2010 5:36 PM fixed code formatting...
    • Marked as answer by Vidmantas Sunday, November 07, 2010 5:44 PM
    Sunday, November 07, 2010 5:35 PM

All replies

  • Vidmantas,

    I think your post may be better suited to one of the MSDN C# forums.  I will be moving your post there, hopefully the members over there will be able to get your problem resolved.  Best regards!

    Sunday, November 07, 2010 5:16 PM
  • Hello,

     

    Try something like this:

          string s = @"cat\temp\sounds\az_A001_l'ai_7a840_.wav";
          string pattern = @"\\([^\\]*).wav";
          string s2 = Regex.Replace(s, pattern, @"\BIN\$1.bin");
    Hope this helps, if you have any other questions or comments, please let me know,


    Best Regards,
    Emanuel Varga

     


    If a post answers your question, please click "Mark As Answer " on that post and "Mark as Helpful ".
    • Edited by Emanuel, Varga Sunday, November 07, 2010 5:36 PM fixed code formatting...
    • Marked as answer by Vidmantas Sunday, November 07, 2010 5:44 PM
    Sunday, November 07, 2010 5:35 PM
  • Thanks Emanuel it works! :).

     

    Sorry for posting in wrong place.. It`s hard to find right place :).

    Sunday, November 07, 2010 5:44 PM
  • Hello,

    Regardless to your actual problem, you can also use 'Regex.Escape(s)'.


    Eyal, Regards.

    commandbrowser.eyal7.net
    Sunday, November 07, 2010 6:11 PM