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
you can use TrimStart
in C#:
string test2 = test.TrimStart()
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();
how about using the trim()
function:
string test = test.trim();
test.trim();
This method removes whitespaces from beginning and ending of the string.
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.
Speaking of Java
: String.trim()
. Here is the link to the API documentation:
String.trim()
I can also use the method TrimStart()
like this:
string test2 = test.TrimStart(' ');
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.