0

I am having a field called Description, which may contain a string which will be greater then n characters. I want to display this description on a VisualForce page.

I want to truncate this discription to 50 characters by taking care of words, word should not be cut.

e.g. if I have a string such as A little lesson in code sometime does not hurt anyoneelse. - the word anyoneelse should not be lost.

It should be displayed as A little lesson in code sometime does not hurt anyoneelse...

3
  • 1
    So what exactly are you after? The code? Are you going this in javascript or apex? What's your approach and what's your current code look like? Please update your question with more relevant information. Apr 22, 2015 at 5:39
  • Make sure the above requirement cannot be implemented using formulas in VF page, to achieve this, a custom logic itself would be required in apex Apr 22, 2015 at 6:07
  • I didnt get the difference between the original sentence & expected sentence.
    – snehakem
    Apr 22, 2015 at 6:13

4 Answers 4

6

You can just use the standard apex String class for this. There's an abbreviate method. See below...

String s = 'Hello Maximillian';
String s2 = s.abbreviate(8);
System.assertEquals('Hello...', s2);
System.assertEquals(8, s2.length());
4

This may be a bad answer .. but I don't think you should do this in Apex or Visualforce. Nor at a specific limit of characters. You'd want to truncate based on design/visual element limitations which may differ on your end user's device, resolution or font size.

---> I would do this client/side with CSS. I'm no CSS pro or webdesigner, but with some google I believe this could be as simple as

text-overflow: ellipsis
1
  • If its purely for display I actually like this one better, there's no chance of the truncated version being saved back to the record.
    – Phil B
    Jan 12, 2017 at 14:07
4

Did some work around and got this

string str = 'Contract effective date should not be later than term end date of subscription.'; string subStr = str.subString(0,50); string finalStr = subStr.subString(0, subStr.lastIndexOf(' ')) + '...';

Thanks for the help

1
  • don't forget to select your own answer as accepted (on the left of it) to show others this is how you solved it. Apr 24, 2015 at 7:29
2

Not sure if I'm following correctly but based on your tags it seems like you're trying to do this in Apex. If that's the case, the below should be a starting point for the algorithm. A whitespace separator is not always going to be the case, sometimes you might have double whitespace, new line etc. so you might need to modify it a little bit in order to accommodate for other scenarios. The steps are explained in the comments:

public String truncateSentence(String sentence, Integer numChars)
{
    // Temporary variable to store the new sentence (word by word)
    String tempSentence = '';

    // For each of the words in the original sentence (split by whitespace)
    for (String word : sentence.split(' '))
    {
        // Check whether our temporary sentence + the new word exceeds the limit of chars
        if (tempSentence.length() + word.length() > numChars)
        {
            // We've reached the max, don't include the next word and return the new sentence
            break;
        }

        // We still haven't reached the max, append the word and keep going
        tempSentence += word + ' ';
    }

    // Return the truncated sentence
    return tempSentence;
}

String mySentence = 'This sentence should be cut at the 22th character';
truncatedSentence = this.truncateSentence(mySentence, 22);
system.debug(truncatedSentence); // This sentence should

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .