How can I remove all white space from the beginning and end of a string?
Like so:
"hello"
returns "hello"
"hello "
returns "hello"
" hello "
returns "hello"
" hello world "
returns "hello world"
How can I remove all white space from the beginning and end of a string?
Like so:
"hello"
returns "hello"
"hello "
returns "hello"
" hello "
returns "hello"
" hello world "
returns "hello world"
String.Trim()
returns a string which equals the input string with all white-spaces trimmed from start and end:
" A String ".Trim() -> "A String"
String.TrimStart()
returns a string with white-spaces trimmed from the start:
" A String ".TrimStart() -> "A String "
String.TrimEnd()
returns a string with white-spaces trimmed from the end:
" A String ".TrimEnd() -> " A String"
None of the methods modify the original string object.
(In some implementations at least, if there are no white-spaces to be trimmed, you get back the same string object you started with:
csharp> string a = "a";
csharp> string trimmed = a.Trim();
csharp> (object) a == (object) trimmed;
returns true
I don t know whether this is guaranteed by the language.)
take a look at Trim()
which returns a new string with whitespace removed from the beginning and end of the string it is called on.
string a = " Hello ";
string trimmed = a.Trim();
trimmed
is now "Hello"
use the String.Trim()
function.
string foo = " hello ";
string bar = foo.Trim();
Console.WriteLine(bar); // writes "hello"
Use String.Trim
method.
String.Trim()
removes all whitespace from the beginning and end of a string.
To remove whitespace inside a string, or normalize whitespace, use a Regular Expression.
Trim()
Removes all leading and trailing white-space characters from the current string.
Trim(Char)
Removes all leading and trailing instances of a character from the current string.
Trim(Char[])
Removes all leading and trailing occurrences of a set of characters specified in an array from the current string.
Look at the following example that I quoted from Microsoft s documentation page.
char[] charsToTrim = { * , , };
string banner = "*** Much Ado About Nothing ***";
string result = banner.Trim(charsToTrim);
Console.WriteLine("Trimmmed
{0}
to
{1} ", banner, result);
// The example displays the following output:
// Trimmmed
// *** Much Ado About Nothing ***
// to
// Much Ado About Nothing
What is the use of default keyword in C#? Is it introduced in C# 3.0 ?
I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...
I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
I have two EF entities. One has a property called HouseNumber. The other has two properties, one called StartHouseNumber and one called EndHouseNumber. I want to create a many to many association ...
How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...
Since I cannot order my dictionary, what is the best way of going about taking key value pairs and also maintaing an index?
Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...