Ellipsis with C# (ending on a full word)

Marko picture Marko · Sep 27, 2010 · Viewed 9.2k times · Source

I'm trying to implement ellipsis in Umbraco, the requirement being 15 characters of intro text but always ending on a full word.

I thought of using XSLT, but then realised that I can use a simple extension method written in C# instead.

I can easily substring the text and append "..." but am stuck with the issue of having to end it with a full word.. Here's my code so far (very complicated :p)

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

Example text

Lorem ipsum dolor sit amet, amet consectetur adipiscing elit.

If we implemented the ellipsis method above (length = 15), the output would look like

Lorem ipsum dolor sit amet, amet consectetur adipi...

I'd like to make sure that no word ever gets sliced, so the output should look like

Lorem ipsum dolor sit amet, amet consectetur adipiscing...

Any help appreciated.

EDIT

After running the sample for the code below, it appears that if the ellipsis length ends on the last word, no text is displayed at all.

Here's the output, see line 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.

Marko

Answer

Preet Sangha picture Preet Sangha · Sep 27, 2010
if (text.Length <= length) return text;
int pos = text.IndexOf(" ", length) ;
if (pos >= 0)
    return text.Substring(0, pos) + "...";
return text;

Edit: This is the program I tested it with:

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

Here is output:

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...