Pages: 1

This is a snippet I use for removing leading and trailing whitespace in a string
Instructions: Need a reference to System.Text.RegularExpressions. Pass the method your string and it will trim it for you
//Namespace Reference
using System.Text.RegularExpressions;
/// <summary>
/// Method for removing trailing and leadingt whitespace from a string
/// </summary>
/// <param name="str">The string to trim</param>
/// <returns></returns>
public string TrimString(string str)
{
try
{
string pattern = @"^[ \t]+|[ \t]+$";
Regex reg = new Regex(pattern, RegexOptions.IgnoreCase);
str = reg.Replace(str, "");
return str;
}
catch (Exception ex)
{
throw ex;
}
}-Offline- |
Pages: 1