Click here to Skip to main content
15,896,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a string which is generated dynamically, the string is basically an JSON string. Each time the string is generated few words in the string gets changing, how can i parse these words so that i replace them with some constant word of my own.

The string looks like this
"end_location":{"Za":17.44716,"$a":78.37790999999993},"start_address":"Mumbai Rd, Madhura Nagar Colony, Madhapur, Hyderabad, Andhra Pradesh, India","start_location":{"Za":17.4243,"$a":78.37761999999998},

With even more data before and after this sample string. Now the result what i get has Za and $a(in bold) which always keeps changing to different value. How can i replace these words with some constant word.
Posted
Updated 20-Apr-12 8:08am
v2

The easiest way of doing it would be by using Regex class.

C#
content = Regex.Replace(content, searchText, replaceText);


For more information on how to use Regex please follow this link[^]
 
Share this answer
 
Comments
manu g m 20-Apr-12 14:40pm    
Thanks for the reply. I can use Regex if i know the exact word, since in my case the words keeps changing i wont know what will be the exact word to replace. I have to replace based on the other supporting characters like {,:, "".
Nelek 20-Apr-12 16:08pm    
If it is always in the same format, then you can search for ": and check if next letter is a number. If yes, go back to the previous " counting the number of chars between and you have found one of the words you are looking for.
Eugene Sadovoi 20-Apr-12 16:08pm    
Regex allows you to search for patterns. Something like this: a text between either characters {,[ and ],} with text being just numbers.
What I am trying to say Regex is a powerful search and replace engine with its own small language.
Eugene Sadovoi 20-Apr-12 16:17pm    
You could post 2 - 3 samples of the source string and what you wanted it to look like and we could help you with the patern.
Thanks for the helpfull answers.
I solved the problem in this manner. First i used regex to identify the changed keywords, once the keywords are known i replaced these with required keywords of my own.
 
Share this answer
 

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