public static Crop ( string strVar, int xNumber, CropType cropType, string xChars, string endWhenCropped ) : String | ||
strVar | string | String to process. |
xNumber | int | Number of words or characters (as close as possible) to return. |
cropType | CropType | Enum value: CropType.Characters or CropType.Words. |
xChars | string | Valid characters on which to break (defaults to space characters). |
endWhenCropped | string | String to append to the return value, only if the string had to be cropped. |
return | String |
public static String Crop(string strVar, int xNumber, CropType cropType, string xChars, string endWhenCropped)
{
string outt = strVar;
if (xNumber > 0)
{
switch (cropType)
{
case CropType.Words:
Regex x = new Regex(@"((\w*?)(\W|\z)){0," + xNumber + @"}", RegexOptions.Singleline);
MatchCollection mc = x.Matches(strVar);
outt = mc[0].ToString();
break;
case CropType.Characters:
string breakOn = xChars;
if (String.IsNullOrEmpty(breakOn))
{
breakOn = " ";
}
int test = 0;
int index = outt.Length;
if (strVar.Length > xNumber)
{
for (int X = 0; X < breakOn.Length; X++)
{
test = outt.IndexOf(breakOn.Substring(X, 1), xNumber);
if (test < index && test >= xNumber )
{
index = test;
}
}
if (index < 1 || index >= outt.Length)
{
index = xNumber;
}
else
{
index++;
}
outt = strVar.Substring(0, index);
}
break;
}
if (outt.Length < strVar.Length)
{
while (outt.Length > 0 && !H3Identify.HasPattern(H3Identify.REGEX_IS_ALPHA_NUMBER_ONLY, outt.Substring(outt.Length-1, 1)))
{
outt = outt.Substring(0, outt.Length - 1);
}
outt += endWhenCropped;
}
}
return outt;
}