tags:

views:

84

answers:

4

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.

+3  A: 

Use the string.Substring overload that takes two arguments, startIndex and length:

s = s.Substring(0, s.Length - 1)
Mark Byers
+3  A: 

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".

jjnguy
A: 

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).

Adkins
+3  A: 

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());
Guffa