Last active
October 4, 2015 20:27
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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