2013-01-21 16 views

cevap

16

Ve yine bir başka olasılık:

Eval("My Text").ToString().PadRight(140).Substring(0,140).TrimEnd() 

Edit: Yine ben LINQ sevmiyorum

:

Eval("My Text").ToString().Take(140).Aggregate("", (x,y) => x + y) 
+1

Bir 'önlemek için (uzunluk> 140)' daha fazla bellek ve zaman kullanırsınız. :) – Aristos

+0

@Aristos Biliyorum, özellikle eğer uzunluk 140'dan küçükse. Ama daha da kötüsü: Orijinal dizgenin ("Metnim") 140 karakterden az olması durumunda, _and_ sonda boşlukları içeriyorsa, bu boşluklar kesilir. Bu çözümün önemi varsa ve her yerde kullanıldıysa ve her bir performans sayımı için bu çözümü tavsiye etmem. Sonra tekrar, daha önce de belirtildiği gibi, "henüz başka bir olasılık". Ve bu sayfadaki en kısa olanı :-) – marapet

+0

Sadece oyun için bir hız testi yapıyorum, ilk fonksiyonunuz 20ms, ikinci 600 ms'nizi alır ve Leniel 5ms alır (50000 karşılaştırmasıyla); – Aristos

2

Sen Kesme yöntemi deneyebilirsiniz:

C# Truncate String

basitçe kaynak parametresi önce this anahtar kelime ekleyerek bir uzantısı yöntemine bunu dönüştürün.

NavigateUrl='<%# Eval("My Text").ToString().Truncate(140) %>' 

Komple konsol test uygulaması:

Bu sizin durumda

, sen olurdu ... Daha dolambaçlı bir yaklaşım ama başka bir yere yeniden gerekiyor durumlarda değerli olabilir
using System; 

namespace ConsoleApplication2 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string test1 = "A really big string that has more than 140 chars. This string is supposed to be trunctaded by the Truncate extension method defined in class StringTool."; 

      Console.WriteLine(test1.Truncate(140)); 

      Console.ReadLine(); 
     } 
    } 

    /// <summary> 
    /// Custom string utility methods. 
    /// </summary> 
    public static class StringTool 
    { 
     /// <summary> 
     /// Get a substring of the first N characters. 
     /// </summary> 
     public static string Truncate(this string source, int length) 
     { 
      if (source.Length > length) 
      { 
       source = source.Substring(0, length); 
      } 
      return source; 
     } 

     /// <summary> 
     /// Get a substring of the first N characters. [Slow] 
     /// </summary> 
     public static string Truncate2(this string source, int length) 
     { 
      return source.Substring(0, Math.Min(length, source.Length)); 
     } 
    } 
} 

Çıktı:

A really big string that has more than 140 chars. This string is supposed to be 
trunctaded by the Truncate extension method defined in class 
3

Lanet gibi LINQ:

string.Concat('<%# Eval("My Text") %>'.ToString().Where((char, index) => index < 140)) 
+0

Ayrıca yazdıklarınızı da beğeniyorum - ama bunun hızı nasıl? Basit bir dize uzunluğu kesmek için çok fazla değil (ve ayrıca yavaş? – Aristos

+1

@Aristos, [Daha hızlı olanı Eric Lippert] (http://ericlippert.com/2012/12/17/performance-rant/). – gdoron

+0

Evet, Hangisi daha hızlı? linq'in 20 satırlık bir tablo üzerinde derlendiğinden emin olun, çünkü eğer statik değilse, o zaman sadece bir dizgiyi kesmek için gerekli olduğunu 20 kere derler. – Aristos

3

Kullanılır (:

< % # Eval("MyText").ToString().Length <= 30 ? Eval("MyText") : Eval("MyText").ToString().Substring(0, 30)+"..." % > 
0

Benzer için Leniel'in cevabı ama bir twist ile .... Bazen görüntülenen dizgenin kesildiğini göstermek için bir elips eklemek istiyorum.

/// <summary> 
    /// Converts the value of the specified string to a truncated string representation 
    /// </summary> 
    /// <param name="source">The specified string</param> 
    /// <param name="length">Integer specifying the maximum number of characters to retain from the specified string.</param> 
    /// <param name="appendEllipsis">Determines whether or not to append an ellipsis to the truncated result. If the specified string is shorter than the length parameter the ellipsis will not be appended in any event.</param> 
    /// <returns>A truncated string representation of the specified string.</returns> 
    public static String Truncate(this String source, int length, bool appendEllipsis = false) 
    { 
     if (source.Length <= length) 
      return source; 

     return (appendEllipsis) 
      ? String.Concat(source.Substring(0, length), "...") 
      : source.Substring(0, length); 
    }