c# - 在 C# 代码隐藏中修剪字符串

标签 c# asp.net code-behind

我的代码隐藏中有这一行:

lblAboutMe.Text = (DT1["UserBody"].ToString());

没问题。但是现在,我们只想显示段落的开头,然后是省略号。所以,而不是:

Please read it all as I hate wasting time with people that don't. If you have an issue with Muslim's please move on as I do not. I used to practice Islam and while I no longer do I still respect it and hate the ignorance people show by following the media or stigma instead of experiencing it or talking with Muslim's to educate themselves about it. Referring to the no drug policy later in this profile, yes, pot/marijuana counts as a drug and is a no with me so please move on. I know what follows makes me seem cold but I am really quite warm and loving, very devoted to the right one, i am just tired of being played and taken for granted/ advantage of. People lie soooo much and ignore so much of what I say I do not want. I have been told many times on here that what I seek is too much.

我们只想取前 100 个字符并在其后加上省略号。所以,像这样:

Please read it all as I hate wasting time with people that don't. If you have an issue with Muslim's please move on as I do not. I used to practice Islam and while I no longer do I still respect it and hate the ignorance people show by following the media or stigma instead of experiencing it or talking with Muslim's to educate themselves ...

我们如何在代码隐藏中做到这一点?我感觉这很容易(因为在 Access 中会很容易),但我对这种语言还是陌生的。

最佳答案

使用 Length 确定您的 string 长度,如果太长则使用 Substring 取其中的一些(100 个字符):

string aboutme = DT1["UserBody"] != null ? DT1["UserBody"].ToString() : ""; //just in case DT1["UserBody"] is null
lblAboutMe.Text = aboutme.Length > 100 ? aboutme.Substring(0,100) + "..." : aboutme;

关于c# - 在 C# 代码隐藏中修剪字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35078040/

相关文章:

c# - 无法在客户端验证电子邮件

c# - 创建和更新 UI 元素 Async Wpf

c# - ASP 中的弹出式警报

asp.net - GridView - 在后面的代码中设置列宽 (AutoGenerateColumns ="true")

.net - 引用 .aspx 页面上的代码隐藏属性?

c# - Linq to SQL C# 获取所有子/子子类别中的产品太慢

c# - Watin引用问题

c# - 如何从给定日期和不包括星期日的日期中找到结束日期

c# - Visual Studio 从一般开发设置更改

c# - 以编程方式在 Datagrid 模板 c# 中添加图像