Sachith MW Sachith MW - 1 month ago 10
C# Question

How to prevent text changing between special symbols

This code i used to text change as i need.

private void textBox1_TextChanged(object sender, EventArgs e)
{
string A = textBox1.Text.Trim();

A = A.Replace("A", "C");
A = A.Replace("F", "H");
A = A.Replace("C", "W");
A = A.Replace("B", "G");

textBox2.Text = (A);
}


Now i need to stop text changing after,
if i type '|' symbol in tetxbox1, Again i need to
start text changing if i type '|' symbol again,such as happened thing in this image.

enter image description here

So how can i prevent text changing between these two symbols only ||

Answer

You're replace code won't work how you have it, as it will just keep changing the characters for the same string(you change A to C, and later down you change C to W, so your final first character would be W and not C like you want).

Below is an overly complicated method(i also added a method that runs through each character of the string doing the replace) but it should work, and you can change as needed. Good luck

private void textBox1_TextChanged(object sender, EventArgs e)
{
       string A = textBox1.Text.Trim();

        string[] Aarry = A.Split('|');
        string cleanedString = "";


        for (int i = 0; i < Aarry.Length; i++)
        {
            if (i % 2 == 0)
                cleanedString += FixText(Aarry[i]) + " ";
            else
                cleanedString += Aarry[i] + " ";
        }

        textBox2.Text = cleanedString ;

The method below will go through each character doing the replace

public string FixText(string A)
    {

        string newText = "";

        for (int i = 0; i < A.Length; i++)
        {
            switch (A.Substring(i, 1))
            {

                case "A":
                    newText += A.Substring(i, 1).Replace("A", "C");
                    break;
                case "F":
                    newText += A.Substring(i, 1).Replace("F", "H");
                    break;
                case "C":
                    newText += A.Substring(i, 1).Replace("C", "W");
                    break;
                case "B":
                    newText += A.Substring(i, 1).Replace("B", "G");
                    break;
                default:
                    break;
            }
        }

        return newText;
    }

To handle the >500 lines of replacement type you have, you could setup a dictionary using method below:

public Dictionary<string, string> ReturnReplacementDictionary()
    {
        Dictionary<string, string> dictLibrary = new Dictionary<string,string)()
        {
            {"A","C"},
            {"F","H"},
            {"C","W"},
            {"B","G"}

        };
        return dictLibrary;
    }

In the above you would just continue adding in all your other replacement values.

Then you would call use that method below instead of the switch case(If you don't add a character/replacement to the dictionary method you can see it will just set the replacement character to blank):

public string FixTextUsingDictionary(string A)
    {
        Dictionary<string, string> replaceDict = ReturnReplacementDictionary();
        string newText = "";

        for (int i = 0; i < A.Length; i++)
        {
            string replacementLetter="";
            if (replaceDict.TryGetValue(A.Substring(i, 1), out replacementLetter)) 
            {
                newText += replacementLetter;
            }
            // Added so that if the char is not in the dictionary the output       will just have the original char
            else { newText += A.Substring(i, 1); }

        }
        return newText;

    }

Good luck