0

I used following code snippet to replace text

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

        A = A.Replace("AB", "CD");
        A = A.Replace("GF", "HI");
        A = A.Replace("AC", "QW");
        A = A.Replace("VB", "GG");

        textBox2.Text = (A);

    }

but i wants to ignore this replace technique within || these symbol.As a example my code do this

when i type AB GF in a txtbox1,txtbox2 replace as following CD HI. Now i need when i type |AB GF| in txtbox1 ,txtbox2 replace as AB GF

i used this code to do this

textBox2.Text = ((B.Contains("|")) ? B.Replace("|", "") : A);

but this isn't work,after | this symbol all containing things in txtbox1 not replaced,how can i do this

Sachith Wickramaarachchi
  • 5,546
  • 6
  • 39
  • 68

1 Answers1

1

Per your comments, you will want to split your string on the spaces prior to doing the replacement. Afterwards you will join it all back together. This is pretty easy with Linq.

public Main()
{
    var strings = new string[]{ "AB GF", "|AB| GF" };
    foreach (var s in strings)
        Console.WriteLine(String.Join(" ", s.Split(' ').Select(x => ReplaceText(x))));
}

string ReplaceText(string text)
{
    if (text.Contains("|"))
        return text.Replace("|", String.Empty);
    else
    {
        text = text.Replace("AB", "CD");
        text = text.Replace("GF", "HI");
        text = text.Replace("AC", "QW");
        return text.Replace("VB", "GG");
    }
}

Prints:

CD HI

AB HI

Looking at your code. If you need to avoid a ReplaceText method. Something like this would work.

string A = textBox1.Text.Trim();

var subStrings = A.Split(' ');
for (int i = 0; i < subStrings.Count(); i++)
{
    if (subStrings[i].Contains("|"))
        subStrings[i] = subStrings[i].Replace("|", String.Empty);
    else
    {
        subStrings[i] = subStrings[i].Replace("AB", "CD");
        subStrings[i] = subStrings[i].Replace("GF", "HI");
        subStrings[i] = subStrings[i].Replace("AC", "QW");
        subStrings[i] = subStrings[i].Replace("VB", "GG");
    }
}

textBox2.Text = String.Join(" ", subStrings);
Derrick Moeller
  • 4,808
  • 2
  • 22
  • 48
  • i have 500 code line such as text = text.Replace("AB", "CD"); then how can i use this technique for all these code,i provide this as a example |AB GF| , i need ignore replace withing this symbols || – Sachith Wickramaarachchi Sep 23 '16 at 17:25
  • I don't understand? I was just using the array to prove it worked for your two examples, it's not part of the logic. You can use your textbox for input. The code above is stripping inputs that contain |, and using the replace functionality for the rest. See the print out "AB GF" became "CD HI" and "|AB GF|" became "AB GF". – Derrick Moeller Sep 23 '16 at 17:29
  • i updated my post,now please see given code... textBox2.Text = ((B.Contains("|")) ? B.Replace("|", "") : A); this code also used between textBox1_TextChanged event – Sachith Wickramaarachchi Sep 23 '16 at 17:30
  • Your code did same thing as my code :( when i type | symbol,all things in a txtbox2 not replaced,i really wants to ignore replace content between only || these two symbols – Sachith Wickramaarachchi Sep 23 '16 at 17:45
  • You must have left something out of your question? What does your input look like? Are "AB GF" and "|AB GF|" possible inputs into textBox1? Or did you have something like "AB GF|AB GF|AC VB" in mind? – Derrick Moeller Sep 23 '16 at 17:53
  • AB GF and |AB GF| is used to provide sample inputs,ok input can be AB GF AC VB,its doesn't matter,problem is when input like this AB GF |AC| VB i dont't need replace text content between || this two symbol, input like : AB GF AC VB output : CD HI QW GG if input like : AB GF |AC| VB i need this output : CD HI AC GG hope you understand my problem :( – Sachith Wickramaarachchi Sep 23 '16 at 18:02
  • It should be working now, in the future you may want to include those details in your question. Accepting a string such as |AB GF| is quite different from AB GF |AB| VB – Derrick Moeller Sep 23 '16 at 18:22
  • Ok dear,it's really working :) great job. Then if i typed like |AB GF| , AB is not replaced but GF is replaced, how can i avoid it,how to stop replace all the content between || these two,not one by one – Sachith Wickramaarachchi Sep 23 '16 at 20:26
  • "|AB GF|" should work, splitting will turn into "|AB" and "GF|" both contain a pipe and will return without being transformed. Something like "|AB AB GF|" would not work, do you need to handle this as well? How many pipes can your input string have? – Derrick Moeller Sep 23 '16 at 20:53
  • You should add some inputs to your question as suggested by jdweng, we need some rules. For example do we need to handle "AB| GF AC|"? Notice the space before GF, how are you deciding what's between the pipes? Is the starting pipe always immediately before two characters? Is the ending pipe always immediately following two characters? Will you ever have an odd number of pipes? How should we handle "AB| GF |GF AC|"? – Derrick Moeller Sep 23 '16 at 21:08
  • I think it's not depend on pipes, this input |AB GF AB AB| makes following output AB HI CD AB ,But i really need this input |AB GF AB AB| for this output AB GF AB AB – Sachith Wickramaarachchi Sep 23 '16 at 21:09
  • what are mentioned by inputs,i'm not understand it :( – Sachith Wickramaarachchi Sep 23 '16 at 21:12
  • "|AB GF|", "|AB| GF", and "|AB GF AB AB|" are all inputs. My original solution addressed the first input, my second solution addressed the first and second inputs. Now you've provided a third input where my solution does not work. We need to understand what inputs the application might see to provide a comprehensive solution. – Derrick Moeller Sep 23 '16 at 21:19
  • see this link http://vikku.info/indian-language-unicode-converter/hindi-unicode-converter.html which is unicode translator,if we type "FrumRoll is good friend" output will be Fरुम्ऱोल्ल् इस् गूड् फ़्रिएन्ड् , if i type "FrumRoll | is good | friend" i need this output " Fरुम्ऱोल्ल् is good फ़्रिएन्ड् " this website similar to my c# programe – Sachith Wickramaarachchi Sep 23 '16 at 21:23