#

:

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

. , , elipse. , , :

, , , . , , , . , , , , . , , / , , , . , , , , /. , , . , , .

, , 100 . , - :

, , , . , , , . , , , , ...

? , ( Access), .

+4
4

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;
+8

String.Substring(startIndex, lenght):

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

MSDN -

, . 100 . shure, :

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;
    }
}
+6

,

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

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

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()) + " ...";
}
+1

Source: https://habr.com/ru/post/1626481/


All Articles