0

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 ||

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68
  • 1
    This should be pretty straightforward. What have you tried so far? – STLDev Sep 24 '16 at 15:26
  • hehe :P i tried most of the codes,and i also asked question yesterday http://stackoverflow.com/questions/39666067/how-can-i-ignore-replace-text-within-specific-two-symbol/39666263?noredirect=1#comment66640996_39666263 but my problem isn't solved :( please help me if you can, my sample codes in the yesterday post – Sachith Wickramaarachchi Sep 24 '16 at 15:31

3 Answers3

1

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

Darthchai
  • 757
  • 8
  • 17
  • this is the solution for my problem, really thank you friend :) but this have small problem,i have 500< lines like this A = A.Replace("A", "C"); A = A.Replace("B", "G"); so,how can i do this without using switch case,have any short method to do this – Sachith Wickramaarachchi Sep 24 '16 at 21:50
  • ok it's working properly,in my code its containing code such as, {"be", "බෙ"}, {"bii", "බී"}, {"bi", "බි"}, {"b", "බ්"}, which are unicode characters,when the input like "bii" output will be "බ්" only,and other thing is any character haven't in the dictionary ( dictLibrary) ,nothing will be displayed in textbox2 :( have any solution ??? – Sachith Wickramaarachchi Sep 25 '16 at 08:45
  • I updated the method above and added the else statement so if there is no match, you will get the original character output – Darthchai Sep 25 '16 at 16:43
  • Thank u very much darthchai :) now one problem is solved :) please tell me how can i use your method if input like this {"AB","L"}, {"FA","FA"}, {"QW","OO"}, {"Q","T"} if input like this {"A","C"}, ,code work properly,now i need the output for input like this {"AS","QW"}, if you can give me your mail, i will send you my private code,plz :( – Sachith Wickramaarachchi Sep 25 '16 at 17:19
  • You could create another method that uses 2 characters out of string, and another method that builds a 2 character dictionary. Then call the 2 character method and if there is a match you add that to your output, and if no match, you call your single character method – Darthchai Sep 26 '16 at 14:29
  • It's too complicated to me Darthchai :( some times it's have 3,4 characters,can i do this without using many more methods,dear can i send my code to you privately – Sachith Wickramaarachchi Sep 26 '16 at 14:36
  • Sorry Sachith I don't have too much time to look through the code. – Darthchai Sep 27 '16 at 00:00
  • Ok Darthchai,Thank you anyway :) – Sachith Wickramaarachchi Sep 27 '16 at 13:27
1

If the text is entered manually not pasted from clipboard, my solution will be:

int counter = 0;
    private string replaceSpecial(string A)
    {
        if (A.Equals("A")) return "C";
        else if (A.Equals("F")) return "H";
        else if (A.Equals("C")) return "W";
        else if (A.Equals("B")) return "G";
        else if (A.Equals("|")) return "";
        else return A;
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar.Equals('|'))
        {
            counter++;
        }
        if (counter == 0 || counter % 2 == 0)
            textBox2.Text += replaceSpecial(e.KeyChar.ToString());
        else
            textBox2.Text += e.KeyChar.ToString().Replace("|", "");
    }

considering that the only entered character is "|". good luck

AhmedAbdelaal
  • 286
  • 2
  • 6
0
void StringReplace(string initialString)
        {
            bool insideSpecialCharacter = false;
            string[] Pattern = { "A-C", "C-W", "F-H", "B-G" };
            string specialCharacter = "|";
            char[] characters = initialString.ToCharArray();

            char?[] Newcharacters = new char?[characters.Length];
            for (int i = 0; i < characters.Length; i++)
            {
                if (!characters[i].ToString().Equals(specialCharacter))
                {
                    if (insideSpecialCharacter)
                    {
                        Newcharacters[i] = characters[i];
                    }
                    else
                    {
                        CheckPattern(Pattern, characters, Newcharacters, i);
                    }
                }
                else
                {
                    insideSpecialCharacter = (insideSpecialCharacter) ? false : true;
                }
            }
            txtSecond.Text = string.Concat(Newcharacters).Trim();
        }

        //-------Checks the Pattern Array and Replaces the Characters-----------

private static void CheckPattern(string[] Pattern, char[] characters, char?[] Newcharacters, int i)
        {
            for (int j = 0; j < Pattern.Length; j++)
            {
                string[] replaceValue = Pattern[j].Split('-');
                if (characters[i].ToString() == replaceValue[0])
                {
                    Newcharacters[i] = Convert.ToChar(characters[i].ToString().Replace(characters[i].ToString(), replaceValue[1]));
                    break;
                }
                else
                {
                    Newcharacters[i] = characters[i];
                }
            }
        }