function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
krishna1krishna1 

How to Remove some text from string

Hi all,

 

we want get only 80 characters from string. If sting have above 80 characters, get only 80 characters from string. How to Remove last characters.

 

Thank you

Krishna

 

Best Answer chosen by Admin (Salesforce Developers) 
Dhaval PanchalDhaval Panchal

Hi

 

You can use str.left(80).

i.e. String str = 'fdsafdsafdsfdsfdsaf';

system.debug('>>>>' + str.left(80));

here you need not to check lenght of str.

 

You can also use Substring method.

 

i.e System.debug('>>>>' + str.substring(0,80));

but it will generate error is length of str is less than 80 so you need to check length before using this

All Answers

Dhaval PanchalDhaval Panchal

Hi

 

You can use str.left(80).

i.e. String str = 'fdsafdsafdsfdsfdsaf';

system.debug('>>>>' + str.left(80));

here you need not to check lenght of str.

 

You can also use Substring method.

 

i.e System.debug('>>>>' + str.substring(0,80));

but it will generate error is length of str is less than 80 so you need to check length before using this

This was selected as the best answer
Satish_SFDCSatish_SFDC
String originalString="A String with more than 80 characters.....";
String myString = originalString.substring(0,80);

The myString now has the first 80 characters.

Hope this helps!
Regards,
Satish Kumar
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.
Satish_SFDCSatish_SFDC
Yes and as dapanchal mentioned, you need to check for the length of the string as well.

Regards,
Satish Kumar