Home > Back-end >  what mistake am I making in this simple wpf app loop in c Sharp?
what mistake am I making in this simple wpf app loop in c Sharp?

Time:09-01

I am trying to develop a very simple and tiny little coding language just as a fun side project. The issue I am currently facing makes me feel stupid. See we have a label wherein the entire code is typed in the program and it is divided into lines of code with the help of String.Split() with a semicolon ';'. It all works till here but then when I try to split each line of code with a dot '.' to see if "print" is one of the elements(say j) and hence get the next element (j 1) added to the output Label Content String. Let me first show the code.

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        String[] lines = code.Text.Split(';');
        String labelContent = "";
        String[] dotSeperated = new string[] { };


        for (int i = 0; i < lines.Length -1; i  )
        {
           dotSeperated = lines[i].Split('.');


            for (int j = 0; j < dotSeperated.Length; j  )
            {
                if(dotSeperated[j] == "print")
                {
                    labelContent  = dotSeperated[j   1];
                }
            }
        }


        

        lb.Content = labelContent;

    }

But when the program is run,

Program Build Screenshot

which is that if I input print.a; print.b;

The output only says 'a' instead of 'ab' Please help me as I can't at all figure out the solution. Thanks a lot in advance.

CodePudding user response:

Output says only 'a' because input string have new line symbol between "print.a;" and "print.b;"

The condition if(dotSeperated[j] == "print") is not satisfied and only 'a' is printed.

Try to change 1st line of code like String[] lines = code.Text.Replace("\n", "").Replace("\r", "").Split(';');

  • Related