Last active
August 23, 2016 08:33
-
-
Save ArseniySavin/bea25442e1a490853c1111cc10e6297d to your computer and use it in GitHub Desktop.
Truncation string
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 TruncateString(this string value, int maxLength) | |
{ | |
int valueLength = value.Length; | |
const string CHARACTER_REPLACE = "..."; | |
int startPosition = (maxLength / 3); | |
if(valueLength > maxLength) | |
{ | |
try | |
{ | |
int modificationLength = ((valueLength - maxLength) + CHARACTER_REPLACE.Length); | |
value = value.Substring(startPosition, modificationLength); | |
} | |
catch(Exception ex) | |
{ | |
throw ex; | |
} | |
} | |
return value; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment