2010-09-27 66 views
8

我試圖在Umbraco中實現省略號,要求是介紹文本的15個字符,但始終以完整的單詞結尾。與C#的省略號(以一個完整的詞結尾)

我想過使用XSLT,但後來意識到我可以使用用C#編寫的簡單擴展方法代替。

我可以很容易地串文字和追加「...」但我堅持與具有一個完整的字來結束它的問題。這裏是我到目前爲止的代碼(很複雜的:P)

public string Ellipsis(string text, int length) 
{ 
    return text.Substring(0, length) + "..."; 
} 

實施例文本

Lorem存有悲坐阿梅特,阿梅特consectetur adipiscing ELIT。

如果我們實現上述省略號方法(長度= 15),輸出將類似於

Lorem存有悲坐阿梅特,阿梅特consectetur adipi ...

我想確保沒有任何詞被切片,所以輸出應該看起來像

Lorem存有悲坐阿梅德,阿梅德consectetur adipiscing ...

任何幫助表示讚賞。

編輯

運行下面的代碼示例後,它看來,如果省略號長度上的最後一個字結束,沒有文字顯示在所有。

這裏的輸出,看線56-60

0 : Lorem... 
1 : Lorem... 
2 : Lorem... 
3 : Lorem... 
4 : Lorem... 
5 : Lorem... 
6 : Lorem ipsum... 
7 : Lorem ipsum... 
8 : Lorem ipsum... 
9 : Lorem ipsum... 
10 : Lorem ipsum... 
11 : Lorem ipsum... 
12 : Lorem ipsum dolor... 
13 : Lorem ipsum dolor... 
14 : Lorem ipsum dolor... 
15 : Lorem ipsum dolor... 
16 : Lorem ipsum dolor... 
17 : Lorem ipsum dolor... 
18 : Lorem ipsum dolor sit... 
19 : Lorem ipsum dolor sit... 
20 : Lorem ipsum dolor sit... 
21 : Lorem ipsum dolor sit... 
22 : Lorem ipsum dolor sit amet,... 
23 : Lorem ipsum dolor sit amet,... 
24 : Lorem ipsum dolor sit amet,... 
25 : Lorem ipsum dolor sit amet,... 
26 : Lorem ipsum dolor sit amet,... 
27 : Lorem ipsum dolor sit amet,... 
28 : Lorem ipsum dolor sit amet, amet... 
29 : Lorem ipsum dolor sit amet, amet... 
30 : Lorem ipsum dolor sit amet, amet... 
31 : Lorem ipsum dolor sit amet, amet... 
32 : Lorem ipsum dolor sit amet, amet... 
33 : Lorem ipsum dolor sit amet, amet consectetur... 
34 : Lorem ipsum dolor sit amet, amet consectetur... 
35 : Lorem ipsum dolor sit amet, amet consectetur... 
36 : Lorem ipsum dolor sit amet, amet consectetur... 
37 : Lorem ipsum dolor sit amet, amet consectetur... 
38 : Lorem ipsum dolor sit amet, amet consectetur... 
39 : Lorem ipsum dolor sit amet, amet consectetur... 
40 : Lorem ipsum dolor sit amet, amet consectetur... 
41 : Lorem ipsum dolor sit amet, amet consectetur... 
42 : Lorem ipsum dolor sit amet, amet consectetur... 
43 : Lorem ipsum dolor sit amet, amet consectetur... 
44 : Lorem ipsum dolor sit amet, amet consectetur... 
45 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
46 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
47 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
48 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
49 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
50 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
51 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
52 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
53 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
54 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
55 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
56 : ... 
57 : ... 
58 : ... 
59 : ... 
60 : ... 
61 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
62 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
63 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
64 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
65 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
66 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
67 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
68 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
69 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
70 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
71 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
72 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
73 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
74 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
75 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
76 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
77 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
78 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 
79 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit. 

馬爾科

+0

您應該使用省略號......而不是三個點 – Dann 2018-03-02 09:57:12

回答

19
if (text.Length <= length) return text; 
int pos = text.IndexOf(" ", length) ; 
if (pos >= 0) 
    return text.Substring(0, pos) + "..."; 
return text; 

編輯:這是程序我測試了它:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 { 
    class Program { 
     static void Main(string[] args) { 
      for (int i = 0; i < 40; ++i) { 
       string s = Ellipsis("Lorem ipsum dolor sit amet, amet consectetur adipiscing elit.", i); 
       Console.WriteLine("{0} : {1}", i, s); 
      } 
     } 

     static public string Ellipsis(string text, int length) { 
      if (text.Length <= length) return text; 
      int pos = text.IndexOf(" ", length) ; 
      if (pos >= 0) 
      return text.Substring(0, pos) + "..."; 
      return text; 
     } 
    } 
} 

這裏輸出:

0 : Lorem... 
1 : Lorem... 
2 : Lorem... 
3 : Lorem... 
4 : Lorem... 
5 : Lorem... 
6 : Lorem ipsum... 
7 : Lorem ipsum... 
8 : Lorem ipsum... 
9 : Lorem ipsum... 
10 : Lorem ipsum... 
11 : Lorem ipsum... 
12 : Lorem ipsum dolor... 
13 : Lorem ipsum dolor... 
14 : Lorem ipsum dolor... 
15 : Lorem ipsum dolor... 
16 : Lorem ipsum dolor... 
17 : Lorem ipsum dolor... 
18 : Lorem ipsum dolor sit... 
19 : Lorem ipsum dolor sit... 
20 : Lorem ipsum dolor sit... 
21 : Lorem ipsum dolor sit... 
22 : Lorem ipsum dolor sit amet,... 
23 : Lorem ipsum dolor sit amet,... 
24 : Lorem ipsum dolor sit amet,... 
25 : Lorem ipsum dolor sit amet,... 
26 : Lorem ipsum dolor sit amet,... 
27 : Lorem ipsum dolor sit amet,... 
28 : Lorem ipsum dolor sit amet, amet... 
29 : Lorem ipsum dolor sit amet, amet... 
30 : Lorem ipsum dolor sit amet, amet... 
31 : Lorem ipsum dolor sit amet, amet... 
32 : Lorem ipsum dolor sit amet, amet... 
33 : Lorem ipsum dolor sit amet, amet consectetur... 
34 : Lorem ipsum dolor sit amet, amet consectetur... 
35 : Lorem ipsum dolor sit amet, amet consectetur... 
36 : Lorem ipsum dolor sit amet, amet consectetur... 
37 : Lorem ipsum dolor sit amet, amet consectetur... 
38 : Lorem ipsum dolor sit amet, amet consectetur... 
39 : Lorem ipsum dolor sit amet, amet consectetur... 
+0

嗨@Preet,其實這也不行,它只是返回**'...'**當我調試, ** pos **變量是-2。 – Marko 2010-09-27 20:14:32

+0

對不起,我沒有一個編譯器方便,現在已經糾正 – 2010-09-27 20:26:47

+0

好得多!謝謝同伴獼猴桃:) – Marko 2010-09-27 20:57:38

3

我想給出一個替代實現,因爲Preet給出的代碼不能確保最大文本長度。我必須實現一個單詞省略號函數,它返回一個長度有限的文本字省略號修整。此外,標點符號也在省略號之前被修剪。

public class WordProcessing 
{ 
    static public string WordEllipsis(string text, int maxLength, string ellipsis = "...") 
    { 
     string result; 

     if (text.Length <= maxLength) 
     { 
      result = text; 
     } 
     else if (maxLength <= ellipsis.Length) 
     { 
      result = ellipsis.Substring(0, maxLength); 
     } 
     else 
     { 
      result = text.Substring(0, maxLength - ellipsis.Length); 
      var lastWordPosition = result.LastIndexOf(' '); 

      if (lastWordPosition < 0) 
      { 
       lastWordPosition = 0; 
      } 
      result = result.Substring(0, lastWordPosition).Trim(new[] { '.', ',', '!', '?' }) + ellipsis; 
     } 

     return result; 
    } 
} 

單元測試 - 作爲一種魅力。

輸出:

0 : 
1 : . 
2 : .. 
3 : ... 
4 : ... 
5 : ... 
6 : ... 
7 : ... 
8 : ... 
9 : Lorem... 
10 : Lorem... 
11 : Lorem... 
12 : Lorem... 
13 : Lorem... 
14 : Lorem... 
15 : Lorem ipsum... 
16 : Lorem ipsum... 
17 : Lorem ipsum... 
18 : Lorem ipsum... 
19 : Lorem ipsum... 
20 : Lorem ipsum... 
21 : Lorem ipsum dolor... 
22 : Lorem ipsum dolor... 
23 : Lorem ipsum dolor... 
24 : Lorem ipsum dolor... 
25 : Lorem ipsum dolor sit... 
26 : Lorem ipsum dolor sit... 
27 : Lorem ipsum dolor sit... 
28 : Lorem ipsum dolor sit... 
29 : Lorem ipsum dolor sit... 
30 : Lorem ipsum dolor sit... 
31 : Lorem ipsum dolor sit amet... 
32 : Lorem ipsum dolor sit amet... 
33 : Lorem ipsum dolor sit amet... 
34 : Lorem ipsum dolor sit amet... 
35 : Lorem ipsum dolor sit amet... 
36 : Lorem ipsum dolor sit amet, amet... 
37 : Lorem ipsum dolor sit amet, amet... 
38 : Lorem ipsum dolor sit amet, amet... 
39 : Lorem ipsum dolor sit amet, amet... 
40 : Lorem ipsum dolor sit amet, amet... 
41 : Lorem ipsum dolor sit amet, amet... 
42 : Lorem ipsum dolor sit amet, amet... 
43 : Lorem ipsum dolor sit amet, amet... 
44 : Lorem ipsum dolor sit amet, amet... 
45 : Lorem ipsum dolor sit amet, amet... 
46 : Lorem ipsum dolor sit amet, amet... 
47 : Lorem ipsum dolor sit amet, amet... 
48 : Lorem ipsum dolor sit amet, amet consectetur... 
49 : Lorem ipsum dolor sit amet, amet consectetur... 
50 : Lorem ipsum dolor sit amet, amet consectetur... 
51 : Lorem ipsum dolor sit amet, amet consectetur... 
52 : Lorem ipsum dolor sit amet, amet consectetur... 
53 : Lorem ipsum dolor sit amet, amet consectetur... 
54 : Lorem ipsum dolor sit amet, amet consectetur... 
55 : Lorem ipsum dolor sit amet, amet consectetur... 
56 : Lorem ipsum dolor sit amet, amet consectetur... 
57 : Lorem ipsum dolor sit amet, amet consectetur... 
58 : Lorem ipsum dolor sit amet, amet consectetur... 
59 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
60 : Lorem ipsum dolor sit amet, amet consectetur adipiscing... 
61 : Lorem ipsum dolor sit amet, amet consectetur adipiscing elit.