Question : Limit visible words asp.net

Hi,

I'm trying to limit the visible words in a text. Usually I'm using a code like this:

Left(strText, 100)

The problem with this is that the text gets cut after 100 tokens even if it's in the middle of a word so it looks very strange.

Is there a way to do it preserving the words?

Peter

Answer : Limit visible words asp.net

There are probably more elaborate ways to do this, but itsuch a pain typing on this tiny screen, so I'll offer this:

Function GetSnippet(string src, int maxlen) As String
{
    Dim temp As New System.Text.StringBuilder();
    Dim words() As String = src.Split(new char() { ' ' });

    For i As Integer = 0 words.Length - 1
        If temp.Length + words[i].Length < maxlen Then
            temp.Append(words(i) & " ")
        Else
            Return temp.ToString().Trim()
        End If
    Next

    Return temp.ToString().Trim()
End Function
Random Solutions  
 
programming4us programming4us