Sunday 27 September 2009

Trim spaces from the start and end of string using C#

In this example I have a text box on my windows form to collect the user input and a label to display the outcome once a button is pressed. The code is as follows to trim spaces from the beginning and end of a user input.

private void button3_Click(object sender, EventArgs e)

{

string userinput;
char [] trimchars = {' '};

userinput = textBox1.Text.Trim(trimchars);

lable1.Text = userinput;

}

No comments:

Post a Comment