79.8k views
3 votes
Given the string variable that follows, which statement removes the spaces from the string and stores the result in the same string variable?

string lastName = " wilson ";
a. lastName = lastName.Trim();
b. lastName = String.Parse(lastName);
c. lastName = lastName.Substring(0, lastName.Length);
d. lastName = lastName.Remove(" ");

1 Answer

4 votes

Final answer:

The correct statement that removes the spaces from the string and stores the result in the same string variable is option a. lastName = lastName.Trim();

Step-by-step explanation:

The correct statement that removes the spaces from the string and stores the result in the same string variable is option a. lastName = lastName.Trim();.

The Trim() method is used to remove leading and trailing white spaces from a string.

For example, if the initial value of lastName is " wilson ", after executing the lastName = lastName.Trim(); statement, the value of lastName will be "wilson" with no spaces.

User Janjonas
by
8.6k points