Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Hi, i have string that contains :

A=B/B=C/C=D.../Z=A


Example : `for (int i = 0; i > myString.lenght; i++)
{
    OriginalString = OriginalString.Replace...` bla bla

And i want to replace Chars by this args how can i do this ??
Thanks.
Posted
Comments
Member 11325242 13-Mar-15 5:59am    
Example 2 :
string Text = "hello";
string args = "H=D/E=A/L=Q/O=Y"

After some codes :)

string output = " Daqqy " !

You could use a dictionary containing the replacement patterns.
Then you must loop through this dictionary
and call the string replace method.

C#
Dictionary<string, string> replacementDictionary = new Dictionary<string, string>();

replacementDictionary.Add("H", "D");
replacementDictionary.Add("E", "A");
replacementDictionary.Add("L", "Q");
replacementDictionary.Add("O", "Y");

string text = "Hello";

foreach(var entry in replacementDictionary)
{
    text = text.Replace(entry.Key, entry.Value);
    text = text.Replace(entry.Key.ToLower(), entry.Value.ToLower());
}

MessageBox.Show(text);
 
Share this answer
 
v3
Comments
Member 11325242 13-Mar-15 6:26am    
Awesome and thanks but if there is little code is better
TheRealSteveJudge 13-Mar-15 6:33am    
Thank you! If you like this solution,
then please do accept it.
Member 11325242 13-Mar-15 6:36am    
I want to create font changer program but if i want to type code from this way i will die but is very helpful code
Split the arguments into a list or table and then apply them one at a time. You will also need to take care that a replace does not corrupt one that has already been applied. For example if you have "H=D/A=Q/D=Z", then it would convert all the H characters to Z.
 
Share this answer
 
Comments
Member 11325242 13-Mar-15 6:12am    
Thanks for answer but can you type with code
Richard MacCutchan 13-Mar-15 6:18am    
Sorry no, that's for you to do.
The solution by TheRealSteveJudge is correct in principle, but it doesn't really tell you how to take your args string to build the replacementDictionary and worse it suffers from the iterative processing of the replacementDictionary values giving a substitution cascade which Richard MacCutchan pointed out.

If your replacements are all single character to single character then the replacementDictionary should be:
C#
Dictionary<char, char> replacementDictionary = new Dictionary<char, char>();

and then you can use LINQ to map through the text to generate the output:
C#
string output = string.Join(string.Empty, text.Select(c => {
    char rep;
    return replacementDictionary.TryGetValue(c, out rep) ? rep : c;
  }));

Now to populate the replacementDictionary from the args:
first you need to split args on the '/'
then for each of those strings, split again on the '=' into a "key" and "value"
set the key/value pairs into the replacementDictionary.
If this is upper/lowercase independent, then first insert the key/value pair with both values as uppercase then repeat with both of them as lowercase. (Use the char.ToUpper(c) or char.ToUpperInvariant(c) as apropriate, and likewise, char.ToLower(c)/char.ToLowerInvariant(c))
 
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