Skip to content

Instantly share code, notes, and snippets.

@sinairv
Last active October 4, 2015 20:27
Show Gist options
  • Save sinairv/2693951 to your computer and use it in GitHub Desktop.
Save sinairv/2693951 to your computer and use it in GitHub Desktop.
A method that trims both white-space and control characters from a string, and its rather fast!
public static string TrimFullString(string str)
{
int start = 0;
int end = str.Length - 1;
for (; start <= end; start++)
{
if (!Char.IsWhiteSpace(str[start]) &&
!Char.IsControl(str[start]))
break;
}
for (; end >= start; end--)
{
if (!Char.IsWhiteSpace(str[end]) &&
!Char.IsControl(str[end]))
break;
}
if (start > end)
return String.Empty;
else
return str.Substring(start, end - start + 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment