Johnny Bones
Johnny Bones

Reputation: 8402

Trimming down a string in C# code-behind

I have this line in my code-behind:

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

No problems. But now, we want to only show the beginning of the paragraph and then an ellipsis. So, instead of being:

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.

We want to take just the first, say, 100 characters and follow it with an ellipsis. So, something like:

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 ...

How can we do this in code-behind? I have a feeling it's pretty easy (because it would be easy in Access), but I'm still new to this language.

Upvotes: 4

Views: 808

Answers (4)

M. Schena
M. Schena

Reputation: 2107

Do it with String.Substring(startIndex, lenght):

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

MSDN - Substring

If you want full words try following code. It determines null values and if it's larger than 100 chars. Than it makes shure a space is at the end:

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

Upvotes: 6

Ian
Ian

Reputation: 30813

Use Length to determine your string length, then use Substring to take some of it (100 chars) if it is too long:

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;

Upvotes: 8

grek40
grek40

Reputation: 13448

Basically, you use Substring but be aware of short texts with less than 100 characters

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

If you want to cut at spaces, use IndexOf or if you want to consider all kinds of whitespace, you can do something along the lines of the following:

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

Upvotes: 1

vikas singh aswal
vikas singh aswal

Reputation: 202

Please also check for Null or empty string

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

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

Upvotes: 1

Related Questions