Mega Code Archive

 
Categories / C# / Data Types
 

Gets the string up to the maximum number of characters

//Microsoft Public License (Ms-PL) //http://visualizer.codeplex.com/license using System; using System.Collections.Generic; namespace Redwerb.BizArk.Core.StringExt {     /// <summary>     /// Provides extension methods for strings.     /// </summary>     public static class StringExt     {         /// <summary>         /// Gets the string up to the maximum number of characters.         /// </summary>         /// <param name="str"></param>         /// <param name="max"></param>         /// <returns></returns>         public static string Max(this string str, int max)         {             return Max(str, max, false);         }              /// <summary>         /// Gets the string up to the maximum number of characters. If ellipses is true and the string is longer than the max, the last 3 chars will be ellipses.         /// </summary>         /// <param name="str"></param>         /// <param name="max"></param>         /// <param name="ellipses"></param>         /// <returns></returns>         public static string Max(this string str, int max, bool ellipses)         {             if (str == null) return null;             if (str.Length <= max) return str;             if (ellipses)                 return str.Substring(0, max - 3) + "...";             else                 return str.Substring(0, max);         }     } }