Using TrimStart and TrimEnd in C# strings

The TrimStart function in C# removes all occurances of a string/character from the beginning of an input string.

string testInput="001";
Int val=Conver.ToInt32(testInput.TrimStart('0'));
Console.WriteLine(val);

Ouput: 1

We can use the TrimEnd function if we need to remove the character at the end of the string. The TrimEnd function in C# removes all occurances of a string/character from the end of an input string.

string testInput="001000000";
Int val=Conver.ToInt32(testInput.TrimEnd('0'));
Console.WriteLine(val);

Output: 001

Share Article/Example:
  • Facebook
  • Twitter
  • del.icio.us
  • Digg
  • DotNetKicks
  • DZone