-2

So Is it possible to remove one letter from a string? For example

String Word = "Hello";

I want to create a function where decrementation happens like if you clicked it it will be Hell then Hel then He then H and "" is it possible?

2 Answers2

0

Something like this?

public string Chopsta(string val) 
   => val?.Length > 0 ? val.Substring(0, val.Length - 1) : val;
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
0

You can easily remove trailing characters from a string by using the string.Remove(int) method. Per the MSDN documentation:

Returns a new string in which all the characters in the current instance, beginning at a specified position and continuing through the last position, have been deleted.

You then assign the return value from the method to your variable to remove the last character every time the button is clicked.

private string sample = "Hello";
private void button_click(object sender, EventArgs e) {
    if (sample.Length > 0)
        sample = sample.Remove(sample.Length - 1);
}

There are many ways to accomplish this task; for more information feel free to perform a Google search on string manipulation in C#.

Hazel へいぜる
  • 2,751
  • 1
  • 12
  • 44