คำตอบ String Replace

  • 20 สิงหาคม 2555 11:40
     
     

    In server.mapth I get

    imagepath=E:\\Imageparse\\Test\\Test\\image\\babby.jpeg

    I want to replace as

    imagepath.Replace("\\Test\\Test","\\Test");

    Can any on help

    Thanks in advance

ตอบทั้งหมด

  • 20 สิงหาคม 2555 16:52
     
     คำตอบ มีโค้ด

    Hello, your code is right. did you try following

    string imagepath="E:\\Imageparse\\Test\\Test\\image\\babby.jpeg"
    
    imagepath = imagepath.Replace("\\Test\\Test","\\Test");

    please Mark as the Answer, If this answers your question. If this post is helpful, please vote as helpful.

  • 20 สิงหาคม 2555 17:47
     
     คำตอบ มีโค้ด

    I would even suggest you to use @ simbol which replaces double \ with a single one:

    string imagepath = @"E:\Imageparse\Test\Test\image\babby.jpeg";
    imagepath = imagepath.Replace("Test\\Test","Test");


    Mitja

  • 20 สิงหาคม 2555 22:23
     
     คำตอบ มีโค้ด

    I would even suggest you to use @ simbol which replaces double \ with a single one:

    string imagepath = @"E:\Imageparse\Test\Test\image\babby.jpeg";
    imagepath = imagepath.Replace("Test\\Test","Test");

    Mitja

    Hi Mitja,

    why not use the @ Symbol in the Raplace-Line then?

    imagepath = imagepath.Replace(@"Test\Test","Test");

    like:

                string s = @"a\b\test\test\c\d";
    
                s = s.Replace(@"\test\test", @"\test");
    
                MessageBox.Show(s);

    Regards,

      Thorsten

  • 21 สิงหาคม 2555 3:24
     
     คำตอบ มีโค้ด

    You must aware that if you look at variable values in Visual Studio's Debug mode.
    You will see double backslash in C# environment.

    For example, if the variable has value "a\b\c",
    While Visual Studio will display it as "a\\b\\c", the actual value is still "a\b\c".

    From your code I guess that it should be:

    string imagePath; // use camelCase for local variables.
    
    imagePath = Server.MapPath("~\test\image\babby.jpeg");
    
    // you must assign the result of Replace back to imagePath
    imagePath = imagePath.Replace(@"\test\test", @"\test");

    HTH



  • 21 สิงหาคม 2555 7:42
     
      มีโค้ด

    why not use the @ Symbol in the Raplace-Line then?

    Ups, I meant that too, but I forgot to change it. Thx for remining me!

    string imagepath = @"E:\Imageparse\Test\Test\image\babby.jpeg";
    imagepath = imagepath.Replace(@"Test\Test","Test");


    Mitja

  • 21 สิงหาคม 2555 18:51
     
     

    Maybe you can avoid replacing here, by adjusting the code that executes ‘MapPath’, so that you will get the required path.


    • แก้ไขโดย Viorel_MVP 21 สิงหาคม 2555 18:51
    •