如何防止特殊符号之间的文本更改

时间:2016-09-24 15:24:11

标签: c# replace symbols textchanged

此代码我习惯于根据需要进行文本更改。

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);
}

现在我需要停止文本更改,             如果我输入' |'在tetxbox1中的符号,我需要             如果我输入' |'再次符号,例如此图像中发生的事情。

enter image description here

那么我怎样才能防止这两个符号之间的文本发生变化||

3 个答案:

答案 0 :(得分:1)

您需要更换无法工作的代码,因为它会不断更改相同字符串的字符(您将A更改为C,稍后将C更改为W,所以你最后的第一个角色是W而不是你想要的C。

下面是一个过于复杂的方法(我还添加了一个方法,它贯穿执行替换的字符串的每个字符),但它应该可以工作,并且您可以根据需要进行更改。祝你好运

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 ;

下面的方法将遍历每个执行替换的角色

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

要处理您拥有的&gt; 500行替换类型,您可以使用以下方法设置字典:

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

        };
        return dictLibrary;
    }

在上文中,您只需继续添加所有其他替换值。

然后你会调用下面的方法而不是switch case(如果你没有在字典方法中添加字符/替换,你可以看到它只是将替换字符设置为空白):

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;

    }
祝你好运

答案 1 :(得分:1)

如果手动输入文本而不是从剪贴板粘贴,我的解决方案将是:

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("|", "");
    }

考虑到唯一输入的字符是“|”。 祝你好运

答案 2 :(得分: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];
                }
            }
        }