简体   繁体   中英

How can I replace some values in a string that contains backslashes using C#?

I have the following:

var topic = "<option value=\"123\">X</option>\r\n  <option value=\"456\">XX</option>\r\n";

And a string that looks like this:

var topicValue = "456";

Is there a way that I can create a new topic string that looks like the following when the topicValue matches:

var topic = "<option value=\"123\">X</option>\r\n  <option value=\"456\" selected=\"selected\" >XX</option>\r\n";

What I am not sure of is how to create a regular expression that will work with the backslashes that are in my topic string. Also is there an easier way to do this than using a regulare expression?

It's never a good idea to manipulate an XML as a string. it looks like you have a part of an XML in topic . Why don't you treat it like an XML. First let's make it a valid XML, just to work with.

        <options>
              <option value=\"123\" isSelectd=\"false\">X</option>
              <option value=\"456\" isSelectd=\"false\">XX</option>
        </options>

Root node <options> is added here and also a new attribute isSelectd which is initially false for both child nodes. The node have value of 456 should be searched and updated. Now using the XmlDocument class you can easily achieve what you want like this.

        var topic = "<options>" + 
               "<option value=\"123\" isSelectd=\"false\">X</option>" +
               "<option value=\"456\" isSelectd=\"false\">XX</option>" + 
               "</options>";

        int selectedValue = 456;

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(topic);
        foreach (XmlNode node in xmlDoc.ChildNodes[0].ChildNodes)
        {
            int value = Convert.ToInt32(node.Attributes[0].Value.ToString());
            if (value == selectedValue)
                node.Attributes[1].Value = "ture";
        }

        topic = xmlDoc.InnerXml;

topic now contains the following XML,

        <options>
              <option value=\"123\" isSelectd=\"false\">X</option>
              <option value=\"456\" isSelectd=\"true\">XX</option>
        </options>

Write it as just how it is was written. For example:

        string myString = "My name is \"John\"";
        Console.WriteLine(myString);
        myString = myString.Replace("\"John\"", "\"Jony\"");
        Console.WriteLine(myString);
        Console.ReadLine();

Try code below to achive what you want:

        string topic = "<option value=\"123\">X</option>\r\n  <option value=\"456\">XX</option>\r\n";
        Console.WriteLine(topic);
        string topicValue = "456";
        string mustBeReplaced = string.Empty;
        string replaceResult = string.Empty;

        if (topic.Contains(topicValue))
        {
            mustBeReplaced = "value=\"" + topicValue + "\"";
            replaceResult = mustBeReplaced + " selected=\"selected\"";
            topic = topic.Replace(mustBeReplaced, replaceResult);
        }

        Console.WriteLine(topic);
        Console.ReadLine();

Okay, here is the code. Note that this only works if the whitespace and formatting is consistent:

var topic = // topic here

const string optionFormat = @"<option value=\""{0}\""";

var topicValue = // topic value here

var topicToReplace = String.Format(optionFormat, topicValue);
var replaced = topic.Replace(topicToReplace , topicToReplace + @" selected=\""selected\""");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM