简体   繁体   English

如何使用C#替换包含反斜杠的字符串中的某些值?

[英]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: 当topicValue匹配时,有没有一种方法可以创建一个新的主题字符串,如下所示:

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. 将XML作为字符串操作从来都不是一个好主意。 it looks like you have a part of an XML in topic . 看起来您的topic中包含XML的一部分。 Why don't you treat it like an XML. 您为什么不将其视为XML。 First let's make it a valid XML, just to work with. 首先,让它成为一个有效的XML,以便使用。

        <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. 根节点<options>在此处添加,并且还添加了一个新属性isSelectd ,该属性最初对于两个子节点都是false。 The node have value of 456 should be searched and updated. 456的节点应被搜索和更新。 Now using the XmlDocument class you can easily achieve what you want like this. 现在使用XmlDocument类,您可以轻松实现所需的目标。

        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, 现在,主题包含以下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\""");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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