在C#中以编程方式解析字符串 [英] Parsing a string programmatically in C#

查看:127
本文介绍了在C#中以编程方式解析字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


如何解析此字符串:
字符串配置=<配置>屏幕淡出超时= \"0 \" font = \"Courier New \" fontsize = \"11 \" linesize = \"4 \" theme_color1 = \-1518056 \" theme_color2 = \"-9674238 \< configuration>";

结果字符串应为:

字符串配置=<配置>屏幕淡出超时= \"0 \" font = \"Courier New \" fontsize = \"10 \" linesize = \"2 \" theme_color1 = \-3318056 \" theme_color2 = \"-9655238 \< configuration>";

Hi
How to parse this string:
string config = "<configuration>screen fadetimeout=\"0\" font=\"Courier New\" fontsize=\"11\" linesize=\"4\" theme_color1=\"-1518056\" theme_color2=\"-9674238\" <configuration>";

the resulting string should be:

string config = "<configuration>screen fadetimeout=\"0\" font=\"Courier New\" fontsize=\"10\" linesize=\"2\" theme_color1=\"-3318056\" theme_color2=\"-9655238\" <configuration>";

推荐答案

为什么不按元素拆分元素以创建所有名称/值集合的数组并更改每个字符串.

之后,再次重新创建整个字符串.

您也可以使用正则表达式来执行此操作...:)
Why dont you split the element by space to create an array of all name/value collection and change the value of each those strings.

After that recreate the entire string again.

You can also use regular expression to do this... :)


您可以用"字符分割元素,然后将获得带有参数值对的数组(例如firs元素是一个参数,第二个是其值,依此类推).之后,您只能修改值并重新构造新的配置字符串.如果未严格定义参数的顺序(即 fadetimeout = \"0 \" 也可以位于字符串中的不同位置),则可以使用Dictionary.这是一个简单的示例:

You could split your elements by " character and you will get an array with param-value pairs (for example the firs element is a param and the second is its value and so on). After that you could modify only the values and reconstruct the new config string. If the order of your parameters is not strictly defined (i.e. if fadetimeout=\"0\" could also be at different positions in the string) than you could use a Dictionary. Here is a quick example:

string config = "screen fadetimeout=\"0\" font=\"Courier New\" fontsize=\"11\"" + 
                "linesize=\"4\" theme_color1=\"-1518056\" theme_color2=\"-9674238\" ";

string[] parts = config.Split(''\"'');
parts[0] = parts[0].Substring(parts[0].IndexOf("screen") + 6);
            
// Fill elements in dictionary as param-value pair
Dictionary<string, string> dict = new Dictionary<string, string>();
for (int i = 0; i < parts.Length - 1; i += 2)
{
     dict.Add(parts[i].Trim(), parts[i + 1]);
}

// Change params 
dict["fontsize="] = "10";
dict["linesize="] = "2";
dict["theme_color1="] = "-3318056";
dict["theme_color2="] = "-9655238";

// Construct the new config string
StringBuilder strb = new StringBuilder("screen ");
foreach(string key in dict.Keys)
{
     strb.Append(key);
     strb.Append("\"" + dict[key] + "\" ");
}

// Get the new string
string newconfig = strb.ToString();



我希望这有帮助. :)
关于



I hope this helps. :)
Regards


在其周围放尖括号,并使用Linq-to-XML像XML一样对其进行解析.就像对待属性一样对待一切.

Put pointy brackets around it and parse it like XML with Linq-to-XML. Just treat everything like an Attribute.

// our original string
string text = "screen_fadetimeout=\"0\" font=\"Courier New\" fontsize=\"11\" linesize=\"4\" theme_color1=\"-1518056\" theme_color2=\"-9674238\"";
// add our XML endcaps
text = string.Format("<TEST {0} />", text);
//Parse the string into an XML element
XElement element = XElement.Parse(text);
// change the value(s) we want to change
element.Attribute("screen_fadetimeout").Value = "5";
// return our string to its original configuration
text = element.ToString().Replace("<TEST","").Replace("/>","").Trim();



顺便说一句,您的示例字符串具有屏幕淡出超时",并且属性名称中不能包含空格,因此我在示例中对其进行了修复.



BTW, your example string had "screen fadetimeout" and you can''t have spaces in the attribute names, so I fixed it in my example.


这篇关于在C#中以编程方式解析字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆