Saturday, March 31, 2012

trim large text

Hi! I have a function that trim large text, but the problem is that it cuts off the last word, so it's not making sense. Is there a function that trim large text in a nice way, making sure the last word is preserved. Thanks for any tip!

Can you just use Trim(myString) ?


You could also use Regular expressions :

using System.Text.RegularExpressions;

private string RemoveSpaces(string str)
{
string result = "";
Regex regulEx = new Regex(@."[\s]+");
result = regulEx.Replace(str," ");
return result;
}

Original code :Laxshmi

HTH,
Suprotim Agarwal

--
http://www.dotnetcurry.com
--



I probably didn't explain what I am doing. If I have a large text to pull from the database and I 'd like just a small portion to be displayed in gridview cell, I am uisng:

Substring(0,Math.Min(100, Eval("Bulletin").ToString().Length)) but this cuts off any last words.


Thank you for the reply! I am looking to shorten text (pulled out of the database)for display in gridview.

Cheers!


:)

Here you go. Even though this is asp code, you will get the idea

http://www.codeproject.com/asp/textpreview.asp

HTH,
Suprotim Agarwal

--
http://www.dotnetcurry.com
--


James,

if I understand you right then you're saying that you want the last word within the 100 character max limit to be preserved and to not be chopped in two, right? Here's a bit of code that might accomplish it for you if that is what you're looking for. It just involves string manipulations is all.

string myStr ="This string should be cut off at the 25th character, and preserve the final word.";string myStr25 = myStr.Substring(0, 25);int lastIdx = myStr25.LastIndexOf(" ");string finalStr = myStr25.Substring(0, lastIdx);

Let me know if you need help from there, but you should be able to adapt it to your need.


If you don't want to cut off words, just trim to the last index of a space.

string text = Eval("Bulletin").ToString();

if (text.Length > 100)

{

text = text.Substring(0, 100);

text = text.Substring(0, text.LastIndexOf(' ') + 1);

}


or how about trying this :

<ItemTemplate>
<%# Eval("Bulletin").ToString().Substring(0, 20) + "..." %>
</ItemTemplate>

Just make sure that all the strings are above 20 characters.

Edit : This code would probably cut off the last words. I do not have my visual studio with me, but what you can do is use this code with the suggestion posted by other members to get your solution.

HTH,
Suprotim Agarwal

--
http://www.dotnetcurry.com
--


I did this but it gave me 95 instead of my text:

<%# Eval("Bulletin").ToString().Substring(0,100).LastIndexOf(" ")%>

I am doing it in the aspx, makes it harder for me.


This is my datalist:

<asp:DataListID="dlBulletins"runat="server">

<ItemTemplate>

<strong><%# Eval("DisplayName")%></strong>

<br/>

<%#formatDate((DateTime)Eval("Bulletin_Date"))%><br/>

<%# Eval("Bulletin").ToString().Substring(0,Math.Min(100, Eval("Bulletin").ToString().Length)) %>

</ItemTemplate>

How can I use the string text in datalist template?

Cheers!

<separatorTemplate><hrnoshade="noshade"size="1"/>

</separatorTemplate>

</asp:DataList></p>


I am using this but it's only returning the first letter!!

<%# Eval("Bulletin").ToString().Substring(0,100).Substring(0,Eval("Bulletin").ToString().LastIndexOf(' ')) %>


It sounds like it's impossible to do this in the datalist!


Thanks friend!! I don't know what i was thinking, I made a function in the code behind called trimtext, then used it in the aspx. Work great now!

Cheers!

0 comments:

Post a Comment