Trim String Method in C#
Triming String in C# , some times we need it for white speaces or
cut some characters from our text.
there is 3 methods :
Trim()
TrimEnd()
TrimStart()
Removes trailing punctuation and white-space characters from the current String object.
Examples
The following example uses the TrimStart method to trim white space.
string str="ABC ";
string trimedStr=str.TrimEnd();
Result will be : "ABC".
Same we can use TrimStart() method to trim and characters we dont need .
one more Example :
I we need to trim two characters in the same time :
string lines="// This code displays a simple greetingsring newLine=line.TrimStart(' ', '/');
Example with Array:
string[] items = new string[] { "MyBlog?", "Hi..." };
// Loop and call TrimEnd.
foreach (string item in items)
{ string trimmed = item.TrimEnd('?', '.'); //Here will remove (? and . )
}
Result :
MyBlog
Hi
I wish this simple code is useful
Share