How to delete the last element of a string
.
If 'globe'
is the value given by user, how to store it as 'glob'
.
That is excluding last element.
How to delete the last element of a string
.
If 'globe'
is the value given by user, how to store it as 'glob'
.
That is excluding last element.
Use the string.Substring overload that takes two arguments, startIndex and length:
s = s.Substring(0, s.Length - 1)
You can use the string.Substring()
method. Pass in the start of the string (0
) and the length you want (Length - 1
).
string globe = "globe";
string glob = globe.Substring(0, globe.Length - 1);
The resulting string glob
will now be "glob"
.
just use the Substring method. You will want to use 0 for the starting value and s.Length - 1 for the amount of the string to be used (assuming s is the name of your string).
There are many ways. For example:
You can use the Substring
method:
string first = original.Substring(0, original.Length - 1);
You can use the Remove
method:
string first = original.Remove(original.Length - 1);
You can use the Take
method:
string first = new String(original.Take(original.Length - 1).ToArray());
You can use the TakeWhile
method:
string first = new String(original.TakeWhile((c,i) => i < original.Length - 1).ToArray());