M
M
Mister_krid2020-12-17 14:13:45
C++ / C#
Mister_krid, 2020-12-17 14:13:45

How to do a string.Remove() check?

How to do string.Remove() check. If the string length is less than the specified length (I do not want to check the string in advance through if). Is it possible to implement this by working with string.Remove(). When the string is less than the specified length, it gives an error
Here is a piece of code if necessary:

static void Main(string[] args)
        {
            Console.WriteLine("sum a1 a2 .. an");
        repeat: //повторение цикла при отсутствии параметров 


            string str1 = Console.ReadLine();
             
            if(str1.Remove(3) == "sum")
            {
                if (str1 == "sum /?")
                {
                    Console.WriteLine("Введите параметры в виде: sum a1 a2 .. an ");
                    goto repeat;
                }
                Console.WriteLine("Сумма = " + Sum(str1));
                return;
            }
            Console.WriteLine("Введены неверные данные, повторите попытку ");
            goto repeat;
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2020-12-17
@Mister_krid

1. You can't. This is not the responsibility of string.Remove => there are no such parameters.
2. goto is not needed in your code. Can be replaced with a normal cycle.

F
freeExec, 2020-12-17
@freeExec

Find out the length of a stringstr1.Length

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question