Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
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>";
Posted

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... :)
 
Share this answer
 
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
 
Share this answer
 
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.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900