2009-09-25 5 views
1

J'ai un grand champ de texte tiré à partir d'une base de donnéescordes Garniture par tant de caractères, mais ne coupez pas le dernier mot

rs.Item("content") 

Comment puis-je limiter à dire 100 caractères, mais pas coupé le dernier mot. par exemple "limiter ceci à 100 cha ..."

Id aiment ajouter le ... sur la fin également.

Merci

+0

Doublons avec http://stackoverflow.com/questions/1458622/string-manipulationasp-net-mvc/1458641#1458641 –

Répondre

1

J'utilise la méthode suivante:

''' <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 
1

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

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

Bonté,

Dan

+0

Edité ... vous avez vu juste voulu VB.NET et non C# –

+0

Je ne pense que cela fonctionne. La fonction "TakeWhile" rend la fonction "Take" non pertinente. Donc, il obtient juste le texte avant le premier espace. – Phil

0

Voici mon C# méthode d'extension

/// <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); 
    } 
0

On dirait que vous pouvez frapper avec quelque chose comme ça, qui ne serait pas coupé le dernier mot en morceaux. Les autres solutions semblent mener à des "Sentences like th ..." qui ne cassent pas les limites des mots, mais je l'ai peut-être manqué car je ne l'ai pas vérifié attentivement.

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(); 
Questions connexes