Names: Using String Functions
Split name into first and last
Names: Using String Functions
Split name into first and last
Start a new project named Names. Build the form as shown below:
- There is a label lblInstructions with the words "Enter a name as last,first"
- There is a text box named txtInput.
- There is a button, btnOK.
- There is a label named lblOutput.
- Set the accept button for the form to btnOK.
4. Write the code as shown below:
private void btnOK_Click(object sender, EventArgs e)
{
String s = txtInput.Text;
// Split name on comma.
String[] parts= s.Split(',');
// parts[0] will be last name, parts[1] will be first name.
// Set output to "Hello " + first name + " " + last name.
lblOutput.Text = "Hello " + parts[1] + " " + parts[0];
}