G
G
Georgy Kuznetsov2020-11-21 22:14:12
C++ / C#
Georgy Kuznetsov, 2020-11-21 22:14:12

How to remove the last character of a string without using the Remove method?

Suppose we have code that will throw an argumentoutofrangeexception if the length of the string exceeds the maximum allowed value of the int type, since there is no override for the Remove method for larger types of variables than int:

if (_input[0] == _input[_input.Length - 1]) {
_input = _input.Remove(0, 1);
_input = _input.Remove(_input.Length - 1); // 1. Required to be replaced
}

Question: Is there a method for String that can replace a line marked with a comment if the code below is guaranteed to print false to the console?

Console.Writeline(Int32.MaxValue >= _input.Length );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
tukreb, 2020-11-21
@JoeSmith3100

You can turn it into StringBuilder https://docs.microsoft.com/en-us/dotnet/api/system...
And then do this

StringBuilder sb = new StringBuilder();
sb.Length--; //удаляем последний символ

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question