Good day.

I need to know exactly which char character is used to separate the string in order to further use this information. Of course, when I enter the string "2 + 3" into the console, I understand that the '+' symbol will be used to split the line into two monomials, but how to get this information from the code itself, to save it and use it in the future?

Char[] operators = { '+', '-', '*', '/' }; String[] monomials = term.Split(operators); Input operands = new Input(); int monomialsLenght = monomials.Length; switch (monomialsLenght) { case 2: { operands.Operand1 = monomials[0]; operands.operand2 = monomials[1]; if (monomials[0] == "" || monomials[1] == "") { ErrReport(); GetTerm(); } break; 

Thank you.

    1 answer 1

    If you can select lines, then what is the problem to find the character between them? If you can’t, then how do you yourself understand that some character is a separator without first specifying line separators?

    • Get, for example, user enters the string "2 + 3", my program divides this line into two monomials (monomials) at position 0 is "2", and at position 1 is "3". I understand that I have previously specified four delimiters: '+', '-', '*', '/'. I need to recognize which of the four is being used, which would later call the appropriate method: add, sub, mul, div. To be honest, I read your answer 5 times, but I didn’t fully understand what you mean. Thanks for the answer. - a66ac
    • 1. Find a symbol in the separator array that matches the one between the lines (2 and 3 in this example). 2. On the index of the found element, call the desired function. - Get
    • Suppose there is a string str, selected from it a line, over which it is necessary to perform the operation (str1 and str2). Also got what is between them - '+'. As in your example, the '+' character is Char [0]. We start an array with the list of functions math_functions [], where in math_functions [0] we specify the name of the addition function, for example, my_sum. Then, knowing the name of the function, we call it for the already known elements of the string (my_sum (str1, str2)). Instead of '+', thanks to this solution, you can use any characters or character sets, for example, 'fold'. - Get
    • Thanks, @get, I went to experiment. - a66ac