Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 382
Johnny Bones Avatar asked Dec 04 '22 01:12

Johnny Bones


1 Answers

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;
like image 160
Ian Avatar answered Dec 22 '22 04:12

Ian