2013-01-21 23 views
8

我與導航屬性設置這樣一個超級鏈接:如何限制文本字符串中的評估和演示

NavigateUrl='<%# Eval("My Text") %>' 

如何限制字符串爲140個字符? 我試過這個Eval(「My Text」)。ToString()。Substring(0,140)但是如果字符串長度少於140個字符,它會引發異常。

+2

也許寫一個擴展方法? –

回答

16

然而另一可能性:

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

編輯:

我喜歡LINQ,太:

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

爲了避免'如果(長度> 140)'你使用更多的內存和時間。 :) – Aristos

+0

@Aristos我知道,尤其是如果長度小於140.但更糟糕的是:如果原始字符串(「我的文本」)長度小於140個字符_and_包含尾隨空格,那些尾部空格將被截斷。如果這些空間很重要,並且在任何地方都可以使用,並且每一點性能都很重要,我不會推薦這種解決方案。然後再次提到,這只是「另一種可能性」。它是這個頁面上最短的一個:​​-) – marapet

+0

僅供玩遊戲我做一個速度測試,你的第一個函數需要20ms,你的第二個600ms,Leniel需要5ms(在50000比較); – Aristos

2

你可以嘗試截斷方法,如下所示:

C# Truncate String

由源參數之前簡單地增加this關鍵字將其轉換爲一個擴展方法。這是一個比較繞口的方法,但可能是在價值的情況下,你需要重新使用別的地方...

在你的情況,你必須:

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

完全控制檯測試應用程序:

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

輸出:

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

哎呀我喜歡LINQ:

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

我也喜歡你的類型 - 但它的速度如何?是不是太簡單的字符串長度切割(也慢?) – Aristos

+1

@Aristos,[Eric Lippert更快](http://ericlippert.com/2012/12/17/performance-rant/)。 – gdoron

+0

是的,哪個更快?肯定linq在20行的表上編譯比較慢,因爲如果它不是靜態的,那麼編譯20次就知道需要剪切一個字符串。 – Aristos

3

使用它(:

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

類似至 Leniel的答案,但扭曲....有時我喜歡追加一個省略號來演示顯示的字符串已被截斷。

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