获得前25个字符的微小方法
任何人都可以想到一个更好的方法来做到以下几点:
public string ShortDescription { get { return this.Description.Length <= 25 ? this.Description : this.Description.Substring(0, 25) + "..."; } }
我本来希望只做string.Substring(0,25),但如果字符串小于提供的长度,它会引发exception。
我经常需要这个,我为它写了一个扩展方法:
public static class StringExtensions { public static string SafeSubstring(this string input, int startIndex, int length, string suffix) { // Todo: Check that startIndex + length does not cause an arithmetic overflow - not that this is likely, but still... if (input.Length >= (startIndex + length)) { if (suffix == null) suffix = string.Empty; return input.Substring(startIndex, length) + suffix; } else { if (input.Length > startIndex) { return input.Substring(startIndex); } else { return string.Empty; } } } }
如果你只需要它一次,那就是矫枉过正,但如果你经常需要它,那么它就会派上用场。
编辑:添加了对字符串后缀的支持。 传入“…”,你可以在较短的字符串上得到你的省略号,或者传入string.Empty,没有特殊的后缀。
return this.Description.Substring(0, Math.Min(this.Description.Length, 25));
没有...
部分。 实际上,你的方式可能是最好的。
public static Take(this string s, int i) { if(s.Length <= i) return s else return s.Substring(0, i) + "..." } public string ShortDescription { get { return this.Description.Take(25); } }
你做的方式对我来说似乎很好,除了我会使用幻数25,我将它作为常数。
你真的想把它存放在你的豆子里吗? 大概这是为了显示某处,所以你的渲染器应该是截断而不是数据对象的东西
嗯,我知道已经接受了答案,我可能会因为在这里抛出一个正则表达而被钉死在十字架上,但这就是我通常这样做的方式:
//may return more than 25 characters depending on where in the string 25 characters is at public string ShortDescription(string val) { return Regex.Replace(val, @"(.{25})[^\s]*.*","$1..."); } // stricter version that only returns 25 characters, plus 3 for ... public string ShortDescriptionStrict(string val) { return Regex.Replace(val, @"(.{25}).*","$1..."); }
它有一个很好的附带好处,即不会将一个单词切成两半,因为它总是在第一个空白字符超过25个字符后停止。 (当然,如果你需要它来截断进入数据库的文本,那可能是个问题。
下行,我相信它不是最快的解决方案。
编辑:替换…与“…”,因为不确定这个解决方案是否适用于网络!
没有….这应该是最短的:
public string ShortDescription { get { return Microsoft.VisualBasic.Left(this.Description;} }
我认为这种方法很合理,但我建议做一些调整
- 将幻数移动到
const
或配置值 - 使用常规
if
条件而不是三元运算符 - 使用
string.Format("{0}...")
而不是+ "..."
- 从函数中只有一个返回点
所以:
public string ShortDescription { get { const int SHORT_DESCRIPTION_LENGTH = 25; string _shortDescription = Description; if (Description.Length > SHORT_DESCRIPTION_LENGTH) { _shortDescription = string.Format("{0}...", Description.Substring(0, SHORT_DESCRIPTION_LENGTH)); } return _shortDescription; } }
对于更通用的方法,您可能希望将逻辑移动到扩展方法:
public static string ToTruncated(this string s, int truncateAt) { string truncated = s; if (s.Length > truncateAt) { truncated = string.Format("{0}...", s.Substring(0, truncateAt)); } return truncated; }
编辑
我广泛使用三元运算符,但如果代码变得足够冗长以至于它开始超过120个字符左右,则更愿意避免使用它。 在那种情况下,我想将它包装到多行,所以发现常规if
条件更具可读性。
EDIT2
对于印刷正确性,您还可以考虑使用省略号字符(…)而不是三个点/句点/句号(…)。
一种方法:
int length = Math.Min(Description.Length, 25); return Description.Substring(0, length) + "...";
有两行而不是一行,但更短的:)。
编辑:正如评论中所指出的,这会让你一直……所以答案是错误的。 纠正它意味着我们回到最初的解决方案。
此时,我认为使用字符串扩展是缩短代码的唯一选择。 只有当代码在至少几个地方重复时才有意义……
看起来很好,非常挑剔我会用实体引用"…"
替换“… "…"
我想不出任何,但你的方法可能不是最好的。 您是否在数据对象中添加了表示逻辑? 如果是这样,那么我建议你将该逻辑放在别处,例如带有GetShortStringMethod的静态StringDisplayUtils类(int maxCharsToDisplay,string stringToShorten)。
但是,这种方法也许不是很好。 不同的字体和字符集怎么样? 您必须以像素为单位开始测量实际的字符串长度。 查看winform的Label类上的AutoEllipsis属性(如果使用此项,您将需要将AutoSize设置为false)。 AutoEllipsis属性,如果为true,将缩短字符串并为您添加“…”字符。
我会坚持你所拥有的东西,但作为替代方案,如果你有LINQ对象你可以
new string(this.Description.ToCharArray().Take(25).ToArray()) //And to maintain the ... + (this.Description.Length <= 25 ? String.Empty : "...")
正如其他人所说,你可能想要将25存储在一个常数中
您应该看看是否可以将Microsoft.VisualBasic DLL引用到您的应用程序中,以便您可以使用“左”function。