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

Trimming down a string in C# 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 中会很容易),但我对这门语言还是陌生的。

String.Substring(startIndex, lenght)做:

lblAboutMe.Text = DT1["UserBody"].ToString().Substring(0, 100) + " ...";

MSDN - Substring

如果您想要完整的单词,请尝试以下代码。它确定空值以及它是否大于 100 个字符。比它确保 space 是最后:

int maxLength = 100;
string body = DT1["UserBody"] != null ? DT1["UserBody"].ToString() : "";

if (!string.IsNullOrEmpty(body))
{
    if(body.Length > maxLength)
    {
        body = body.Substring(0, maxLength);
        // if you want to have full words
        if (body.Contains(" "))
        {
            while (body[body.Length - 1] != ' ')
            {
                body = body.Substring(0, body.Length - 1);
                if(body.Length == 2)
                {
                    break;
                }
            }
        }
        lblAboutMe.Text = body + "...";
    }
    else
    {
        lblAboutMe.Text = body;
    }
}

使用 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;

另请检查 Null 或空字符串

 string aboutme = Convert.ToString(DT1["UserBody"]);

    if (!string.IsNullOrEmpty(aboutme))
    {
        lblAboutMe.Text = aboutme.Length > 100 ? aboutme.Substring(0, 100) +"..." : aboutme;
    }

基本上,您使用 Substring 但要注意少于 100 个字符的短文本

string test = /* your full string */;
string result = test.Substring(0, Math.Min(test.Length, 100)) + " ...";

如果你想在空格处剪切,使用IndexOf或者如果你想考虑各种空白,你可以按照以下方式做一些事情:

string result = test.Substring(0, Math.Min(test.Length, 100));
if (test.Length > 100)
{
    result += new string(test.Substring(100).TakeWhile(x => !char.IsWhiteSpace(x)).ToArray()) + " ...";
}