madphp
madphp

Reputation: 1764

Trim String by so many characters but do not cut off last word

I have a large text field taken from a database

rs.Item("content")

How can I limit this to say 100 characters but not cut off the last word. eg "limit this to 100 cha..."

Id like to add the... onto the end also.

Thanks

Upvotes: 1

Views: 2795

Answers (5)

Michael Kennedy
Michael Kennedy

Reputation: 3380

Seems like you could hit it with something like this, which would not cut the last word in to pieces. The other solutions seem to lead to "Sentences like th..." which don't break on word boundaries, but I may have missed it as I didn't check that carefully.

if (text.Length < 100)
    return text;

var words = text.Split(' ');

var sb = new StringBuilder(128);
foreach (string w in words)
{
    if (sb.Length + w.Length > 100)
    {
        sb.Append("...");
        return sb.ToString();
    }
    sb.Append(w).Append(" ");
}

return sb.ToString();

Upvotes: 0

Sergey
Sergey

Reputation: 3213

Here is my C# extension method

    /// <summary>
    /// Creates a shortend version of a string, with optional follow-on characters.
    /// </summary>
    /// <param name="stringToShorten">The string you wish to shorten.</param>
    /// <param name="newLength">
    /// The new length you want the string to be (nearest whole word).
    /// </param>
    public static string ShortenString(this string stringToShorten, int newLength)
    {
        if (newLength > stringToShorten.Length) return stringToShorten;

        int cutOffPoint = stringToShorten.IndexOf(" ", newLength -1);

        if (cutOffPoint <= 0)
            cutOffPoint = stringToShorten.Length;

        return stringToShorten.Substring(0, cutOffPoint);
    }

Upvotes: 0

Zhaph - Ben Duguid
Zhaph - Ben Duguid

Reputation: 26956

I use the following method:

''' <summary>
''' Creates a shortend version of a string, with optional follow-on characters.
''' </summary>
''' <param name="stringToShorten">The string you wish to shorten.</param>
''' <param name="newLength">
''' The new length you want the string to be (nearest whole word).
''' </param>
''' <param name="isAbsoluteLength">
''' If set to <c>true</c> the string will be no longer than <i>newLength</i>.
''' and will cut off mid-word.
''' </param>
''' <param name="stringToAppend">
''' What you'd like on the end of the shorter string to indicate truncation.
''' </param>
''' <returns>The shorter string.</returns>
Public Shared Function ShortenString(stringToShorten As String, newLength As Integer, isAbsoluteLength As Boolean, stringToAppend As String) As String
  If Not isAbsoluteLength AndAlso (newLength + stringToAppend.Length > stringToShorten.Length) Then
    ' requested length plus append will be longer than original
    Return stringToShorten
  ElseIf isAbsoluteLength AndAlso (newLength - stringToAppend.Length > stringToShorten.Length) Then
    ' requested length minus append will be longer than original
    Return stringToShorten
  Else
    Dim cutOffPoint As Integer

    If Not isAbsoluteLength Then
      ' Find the next space after the newLength.
      cutOffPoint = stringToShorten.IndexOf(" ", newLength)
    Else
      ' Just cut the string off at exactly the length required.
      cutOffPoint = newLength - stringToAppend.Length
    End If

    If cutOffPoint <= 0 Then
      cutOffPoint = stringToShorten.Length
    End If

    Return stringToShorten.Substring(0, cutOffPoint) + stringToAppend
  End If
End Function

Upvotes: 1

Daniel Elliott
Daniel Elliott

Reputation: 22857

Dim newString = New String(
                 yourString
                     .ToCharArray()
                     .Take(100)
                     .TakeWhile(Function(c) c <> " "c).ToArray())

Kindness,

Dan

Upvotes: 0

wefwfwefwe
wefwfwefwe

Reputation: 3518

Console.WriteLine(content.Substring(content.IndexOf(" ", 99)) + "...");

Upvotes: 2

Related Questions