none
Remove a string with double quotes RRS feed

  • Question

  • I'd like to remove the following text string between these two tags:

    </tag>" Machines="{}" Serial="False"  /></tag>

    This is my code, the findStr can be found as I debug, but replace it with an empty string does not take place.   I think the problem was it having multiple double quotes.  Any help is appreciated.

                string findText = "</tag>" + '"';
                string findText2 = "</tag>";

                int pos1 = FileContent.IndexOf(findText) + findText.Length;
                int pos2 = FileContent.IndexOf(findText2, pos1);

                string findStr = string.Empty;
                findStr = text.Substring(pos1, pos2 - pos1);
                    
               text.Replace(findStr, "");




    • Edited by Hung Dao Monday, April 13, 2015 4:34 PM simply it
    Wednesday, April 8, 2015 9:50 PM

Answers

  • Hello Hung Dao,

    >>but replace it with an empty string does not take place.

    Do you mean that the “text” does not remove the text string between these two tags? If so, you could have a try write below code:

    text = text.Replace(tempStr, "");

    Please check the Replace method: https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx

    It returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string, so it would not replace its called instance. And I created a test with your code which show it could work:

    string input = "</ArrayOfMachineProperty>\" TargetedMachines=\"{x:Null}\" Serial=\"False\" Description=\"UDS-ApplyGoldStandards\" ActivityId=\"0\" /></ExecuteSequenceActivity>";
    
                    string findText = "</ArrayOfMachineProperty>" + '"';
    
                    string findText2 = "</ExecuteSequenceActivity>";
    
    
                    int pos1 = input.IndexOf(findText) + findText.Length;
    
                    int pos2 = input.IndexOf(findText2, pos1);
    
    
                    string findStr = string.Empty;
    
                    findStr = input.Substring(pos1, pos2 - pos1);
    
    
                    input = input.Replace(findStr, "");
    

    The result is:

    </ArrayOfMachineProperty>"</ExecuteSequenceActivity>

    If I do not understand correctly, please let me know.

    Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Thursday, April 9, 2015 1:49 AM
    Moderator

All replies

  • Hello Hung Dao,

    >>but replace it with an empty string does not take place.

    Do you mean that the “text” does not remove the text string between these two tags? If so, you could have a try write below code:

    text = text.Replace(tempStr, "");

    Please check the Replace method: https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx

    It returns a new string in which all occurrences of a specified string in the current instance are replaced with another specified string, so it would not replace its called instance. And I created a test with your code which show it could work:

    string input = "</ArrayOfMachineProperty>\" TargetedMachines=\"{x:Null}\" Serial=\"False\" Description=\"UDS-ApplyGoldStandards\" ActivityId=\"0\" /></ExecuteSequenceActivity>";
    
                    string findText = "</ArrayOfMachineProperty>" + '"';
    
                    string findText2 = "</ExecuteSequenceActivity>";
    
    
                    int pos1 = input.IndexOf(findText) + findText.Length;
    
                    int pos2 = input.IndexOf(findText2, pos1);
    
    
                    string findStr = string.Empty;
    
                    findStr = input.Substring(pos1, pos2 - pos1);
    
    
                    input = input.Replace(findStr, "");
    

    The result is:

    </ArrayOfMachineProperty>"</ExecuteSequenceActivity>

    If I do not understand correctly, please let me know.

    Regards,

    Fred.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Thursday, April 9, 2015 1:49 AM
    Moderator
  • Use Regex.  See webpage below.  It would probably make more sense to use an XML function except it looks like you are trying to fix a corrupted XML string.

    https://msdn.microsoft.com/en-us/library/az24scfc%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396#substitutions

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string input = "</ArrayOfMachineProperty>\" TargetedMachines=\"{x:Null}\"" +
                               "Serial=\"False\" Description=\"UDS-ApplyGoldStandards\" ActivityId=\"0\" /></ExecuteSequenceActivity>";
                string pattern = "(?'prefix'</ArrayOfMachineProperty>)(?'delete'.*)(?'suffix'</ExecuteSequenceActivity>)";
                string output = Regex.Replace(input, pattern, "${prefix}${suffix}");
    
            }
        }
    }


    jdweng

    Thursday, April 9, 2015 3:02 AM
  • Not sure why regex wouldn't work.  Sometimes with Regex you need break fixes into smaller pieces.  Never expect regex to do everything with only one pattern.

    jdweng

    Thursday, April 9, 2015 3:07 PM