如何使用该字符串中的标记更新长字符串的某个部分 [英] How to I update a certain part of a long string using a tag within that string
本文介绍了如何使用该字符串中的标记更新长字符串的某个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为了进一步澄清我的问题,我有一个非常长的字符串,如下所示:
To further clarify my question I have this very long string that looks like this:
[fied1:"contents of field1 that can contain any kinds of characters with no length limit",field2:"field2 contents",field3:"field3contents and so on", field4:null]
你可以看到我调用了这些字段名称在我的问题中标记以便于搜索,值用引号括起来,每个字段用逗号分隔,现在问题是如何在不触及其他字段的情况下更新字符串上的某个字段,在本例中说我想更新field2的值。什么是我应该采取的最好的行动方案?
谢谢
as you can see there are these field name that i called "tag" in my question for easy searching,the values are enclosed in a quotation mark and each field is separated by a comma, now the question is how can i update a certain field on the string without touching the other fields, in this example say i'd like to update the value of field2. What's the best course of action I should take?
thanks
推荐答案
我可能会使用正则表达式:
I would probably use a regex:
public static Regex replaceField2 = new Regex(
"(?<=field2:\").*?(?=\")",
RegexOptions.IgnoreCase
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
// Replace the matched text in the InputText using the replacement pattern
string result = replaceField2.Replace(InputText,"new value for field2");
这篇关于如何使用该字符串中的标记更新长字符串的某个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文