我怎样才能截断我的琴弦用" ..."如果他们是太长了? [英] How can I truncate my strings with a "..." if they are too long?

查看:174
本文介绍了我怎样才能截断我的琴弦用" ..."如果他们是太长了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
指示截断在ToolTipStatusLabel自动

希望有人具有良好理念。我有一个这样的字符串:

Hope somebody has a good idea. I have strings like this:

abcdefg
abcde
abc

我需要的是对它们进行trucated显示这样如果超过指定lenght更多:

What I need is for them to be trucated to show like this if more than a specified lenght:

abc ..
abc ..
abc

有没有简单的C#代码,我可以使用吗?

Is there any simple C# code I can use for this?

推荐答案

下面是包裹逻辑起来的扩展方法:

Here is the logic wrapped up in an extension method:

public static string Truncate(this string value, int maxChars)
{
    return value.Length <= maxChars ? value : value.Substring(0, maxChars) + "...";
}



用法:

Usage:

var s = "abcdefg";

Console.WriteLine(s.Truncate(3));

这篇关于我怎样才能截断我的琴弦用&QUOT; ...&QUOT;如果他们是太长了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆