Hi,
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
Hi,
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
Console.WriteLine(content.Substring(content.IndexOf(" ", 99)) + "...");
Dim newString = New String(
yourString
.ToCharArray()
.Take(100)
.TakeWhile(Function(c) c <> " "c).ToArray())
Kindness,
Dan
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