0

this is kinda hard to explain, but I will try my best.

So I want to have 3 TextBoxes.

TextBox 1(Input): "X:Y:Z"

TextBox 2(Input): "A:B"

TextBox 3(Output):

Now for each Line I want to Compare Y and A. Each Lines in TextBox1 Looks like this: "ABC:123:DEF" now it should split out 123 and compare it with A, which is splitted from B aswell.

If it equals it should give out in TextBox3: X:B:Z If not it shouldn't give it out in TextBox3.

EXAMPLES:

TB1: abc:123:def 
TB2: 123:bla 
TB3: abc:bla:def
------      
TB1: abc:741:def 
TB2: 345:bla 
TB3: no output

This is what I got:

private void button1_Click(object sender, EventArgs e)
{
    var textToReplace = textBox1.Text.Split(':');
    var replacementArray = textBox2.Text.Split(':');

    if (replacementArray.Length == 2)
    {
        textBox3.Clear();

        for (var i = 0; i < textToReplace.Length; i++)
        {
            if (i > 0)
            {
                textBox3.AppendText(":");
            }

            textBox3.AppendText(textToReplace[i].Replace(replacementArray[0], 
            replacementArray[1]));
        }
    }
}

IMG for 1 Line working:

For the first line it works great but not for the following. Thanks for all the HELP!

17
  • 2
    This is very confusing. Your question says 3 textboxes but you only give 2 examples. I suggest editing to try to make this more clear Commented Sep 21, 2018 at 18:10
  • 1
    Why don't you give a concrete example of what textbox 1 and 2 contains, and what the concrete and correct result in textbox 3 should be. (Please edit in improve your question with this information) Commented Sep 21, 2018 at 18:12
  • 1
    Fair enough, but its still very hard to understand your desired functionality. Maybe some formatting would make it more clear? Commented Sep 21, 2018 at 18:12
  • 1
    You'll need to first split the inputs of the text boxes to separate lines, then apply what you're doing now. Commented Sep 21, 2018 at 18:18
  • 1
    guys this conversation is so off topic xD Commented Sep 21, 2018 at 18:27

3 Answers 3

1

Im not sure what to want to achieve. I understand it this way:

  1. In text box 1 you have some string with token to be replaced.

  2. In text box one you have token and after : is strong to be inserted in token place.

  3. Text box 3 is presenting result.

Basically you want to do string replace. Am I right?

Sample code:

var input = textBox1.Text;
var tokenArr = textBox2.Text.Split(":");
var output = input.Replace(tokenArr[0], tokenArr[1]);
textBox3.Text = output;
Sign up to request clarification or add additional context in comments.

1 Comment

you pretty much got the problem the only thing is that it should scan every line in TB 2 before the ":" and should check if any of these equals TB1 token between both ":" if this equals then it should replace otherwise go to next line
0

What you have is a dictionary in textbox 2, so put in one, then for each line of tb1 you can search the right value.

string tb1 = @"abc:123:def
abc:741:def2
xxx:345:bla3";
string tb2 = @"123:bla
345:bla2";
string tb3="";

var repDic = new Dictionary<string,string>();
foreach(var line in tb2.Split(new string[]{Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries))
{
    var spl = line.Split(':');
    if(!repDic.ContainsKey(spl[0]))
    {
        repDic.Add(spl[0],spl[1]);
    }
}

StringBuilder sb = new StringBuilder();
foreach(var line in tb1.Split(new string[]{Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries))
{
    var spl = line.Split(':');
    string val;
    if(repDic.TryGetValue(spl[1],out val))
    {
        sb.AppendLine($"{spl[0]}:{val}:{spl[2]}");
    }

}
tb3 = sb.ToString();

TextBox3 Output:

abc:bla:def
xxx:bla2:bla3

3 Comments

this was just an example
@Frexith I didn't understood your comment. The code I posted is doing what you want, for each line in TextBox1 it searches a match in all lines of TextBox2, when it finds, it post the replacement in TextBox3. What I did is put an example with fake inputs (tb1, tb2) and a fake output (tb3). Just replace the fakes inputs and outputs with your textBox#.Text
@Frexith Glad to help :)
0

I feel like your code works as you want... except when it has no matches it outputs the original string instead of "nothing". So add...

if (textBox1.Text == textBox3.Text) 
    textBox3.Text = "";

5 Comments

If I put multiple lines it gives out nothing that's a thing
what do you mean multiple lines?
If I put in multiple lines in TB1 & TB2 in TB3 there is no output
if (replacementArray.Length == 2)... this would stop it from working... because if you had two lines with two values each... the array would have 3 values... for example: "abc:456\ndef:752" would split to "abc" and "456\ndef" and "752" where \n is a newline character... note: this is the first you've mentioned about multiple lines, right?
You would first need to do a split on '\n' and then process each separately.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.