Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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]

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
Posted
Updated 27-Apr-14 1:52am
Comments
Kornfeld Eliyahu Peter 27-Apr-14 7:42am    
There is any particular reason to use this format for your string?
phyxian 27-Apr-14 7:52am    
well its a string format produced by an application im using, but that application does not support an update feature so im trying to make one
Kornfeld Eliyahu Peter 27-Apr-14 8:03am    
Why not to break it up to a key-value pair list (http://msdn.microsoft.com/en-us/library/5tbh8a42(v=vs.110).aspx), that play with it as you wish and create the required string format from that list...
phyxian 27-Apr-14 8:16am    
would it still produce the same format as my example? as i said that string format is created by an application so if i update that string it should still be readable by the application that produced it
BillWoodruff 27-Apr-14 9:42am    
You can be sure if you get an answer from OriginalGriff it is a valid answer, and the best answer ! For learning purposes, if you want to see an example of doing this without using RegEx, just ask here.

1 solution

I would probably use a regex:
C#
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");
 
Share this answer
 
Comments
phyxian 27-Apr-14 8:05am    
thanks for that quick answer though i'll try your solution in a little while, since i got something else i need to finish first. :)
gggustafson 27-Apr-14 13:13pm    
+5 as usual
phyxian 27-Apr-14 20:43pm    
@OriginalGriff, I am now currently trying your solution though I can't seem to make it work, is there suppose to be a reference that I should add to my project for it work? thanks again
phyxian 27-Apr-14 21:09pm    
i was now able to make it work, thank you so much. :)
OriginalGriff 28-Apr-14 3:04am    
You're welcome!

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