12

How can I make it that I can reduce the length of a String? Like for example the string.Length() = 300.

My goal is to make it to 120 characters, retaining the left side (e.g. NameOfThePerson to NameofThePers).

string sizeString = 'Imagine this is more than 120 Characters';
if(sizeString.Length() > 120){
    //insert logic here to make it 120 characters
}

6 Answers 6

9

Use substring method of String class

Returns a new String that begins with the character at the specified zero-based startIndex and extends to the character at endIndex - 1.

String sizeString = 'Let\'s Imagine this is more than 120 Characters';
Integer maxSize = 120;
if(sizeString.length() > maxSize ){
    sizeString = sizeString.substring(0, maxSize);
}
3
  • 1
    Wouldn't calling the substring function without the if be sufficient? Because it will just grab as much as it can if it remains lower than 120? Dec 19, 2017 at 15:02
  • @GerritLuimstra I think so, and it might even be faster, since otherwise it would check the same condition two times. (I know that in low level programming languages like C/C++, removing the condition is definitely faster (Because of branch prediction), but maybe in Java, the overhead of calling the substring function outweighs this redundant condition checking).
    – yyny
    Dec 19, 2017 at 15:13
  • 4
    No it will error if you try to take a substring which is longer than the string itself. You should try it if you are unsure.
    – Adrian Larson
    Dec 19, 2017 at 16:15
32

As an indicator to the user that the string has been shortened, also adding '...' to the end can be helpful.

There is a String method that does that:

String newString = sizeString.abbreviate(120);

This example from the help illustrates how the length of the '...' is also considered:

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

Another way to do this, especially addressing the specific question,

My goal is to make it to 120 characters, retaining the left side

Use the left(length) method from the String class

and here's a usage example:

String s1 = 'abcdaacd';
String s2 = 
s1.left(3);
System.assertEquals('abc', s2);

and for your example would be,

string sizeString = 'Imagine this is more than 120 Characters';
string resizedString = '';
if(sizeString.Length() > 120){
    resizedString = sizeString.left(120);
}
2

If you don't want an ellipsis character, use a quick regular expression to match only the first 80 (or whatever number) characters, and replace the string with just those.

// no ellipsis, 120 characters per OP question
yourString.replaceFirst('^(.{120}).*', '$1')
// ellipsis, 120 characters per OP question
yourString.replaceFirst('^(.{117}).*', '$1...')
1
  • 1
    then you'd have to call yourString.abbreviate(83); Jul 21, 2022 at 9:27
0

I use this expression to assign a limited number of characters from the rawNoteTitle to a noteTitle variable for use in an accordion control label:

noteTitle = (rawNoteTitle.length() < 80) ? rawNoteTitle : rawNoteTitle.left(80) + '...';

If the rawNoteTitle is less than 80 characters, then use the rawNoteTitle. Otherwise use the first 80 characters of the rawNoteTitle and add ... to indicate the title was truncated. Of course, you can use a variable parameterize the truncated length.

The selected correct answer above is ok, but it assigns the string variable twice, and uses a less obvious method of truncating the string. It's harder to look at.

The Salesforce explanation of the Ternary operator ?: is here:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_expressions_operators_understanding.htm

2
  • If the maximum length is 80, this approach would fail. It is also rather duplicative of the abbreviate method.
    – Adrian Larson
    Dec 31, 2022 at 5:17
  • Agreed. I was not aware of the abbreviate method. I missed Keith C's solution, which is the best solution posted. Jan 1, 2023 at 15:38
0

If you want to keep it under 15 characters

name.substring(0, Integer.min(15, name.length()))
1
  • 1
    Welcome to Salesforce Stack Exchange (SFSE). That is a nice subtle difference from the accepted answer. But on SFSE the why (explanation) is often as or more important than the how (code) in an answer. ("Teach a man to fish," and all that.) Please edit your answer to add some explanation on how it differs from and possibly improves on the accepted answer.
    – Moonpie
    Oct 5, 2021 at 14:33

You must log in to answer this question.

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