I want to check the size of a string. If the size is greater than 255 characters, then reduce it to 252 and add three dots towards the end.

For example.

String1 = 'Suppose this is 255 characters'; String2 = 'Suppose this is more than 255 character';

I want the output value of String2 to be 'Support this is more than 255 charac...';

I tried the following but it didn't work:

if (String.size() > 255) {
   String3 = String2.abbreviate(255);
  • 5
    String string3 = string2.abbreviate(255); will work. Note that String will be interpreted as a class name in your code so will generate a compile error.
    – Keith C
    Commented Feb 19 at 11:00
  • How do I add three dots (...) towards the end of the string after limiting it to 252 characters?
    – Goat123
    Commented Feb 19 at 11:02
  • 3
    The abbreviate method does that - see its documentation.
    – Keith C
    Commented Feb 19 at 11:05

1 Answer 1


Unless you have a variable defined called String, that will be resolved as the class name String and your code won't compile.

Code like this:

String string2 = 'Long string...';
String string3 = string2.abbreviate(255);

will ensure the resulting string is no more than 255 characters long, and will add three dots "..." if the input string exceeds 255 characters. So you will get 252 characters from the input string and the last 3 characters replaced by the dots.

See the abbreviate method documentation.

You must log in to answer this question.

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