How do I remove whitespace at the beginning of my

2019-07-29 05:43发布

问题:

Let's say I have following string: string test = " False";

I can't loop the characters of the string because I need to do this for 1744 strings, then it would be a lot of work.

Do you know if there is a method from Microsoft that I can use to delete this whitespace?

Like this: string test2 = test.DeleteFirstWhitespace();

Thanks

回答1:

you can use TrimStart in C#:

string test2 = test.TrimStart()


回答2:

As everyone else has pointed out, there is a trim function. Make sure to remember that a string is immutable, so when you call test.Trim(), it will not modify the test variable, it will return a new string:


string trimmed = test.Trim();
// or
string trimmed = test.TrimStart();



回答3:

how about using the trim() function:

string test = test.trim();


回答4:

test.trim();
This method removes whitespaces from beginning and ending of the string.



回答5:

You can use:

  • String.Trim() to remove all leading and trailing white-space characters
  • String.TrimStart() to remove only leading white-space characters
  • String.TrimEnd() to remove only trailing white-space characters

All these have additional overloads which let you specify an array of custom characters also to be removed.



回答6:

Speaking of Java: String.trim(). Here is the link to the API documentation: String.trim()



回答7:

I can also use the method TrimStart()

like this:

string test2 = test.TrimStart(' ');



回答8:

In java:

Use trim() to remove the whitespaces around a String:

String test2 = test.trim();

Returns a copy of the string, with leading and trailing whitespace omitted.