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
Kiran.sfdc14Kiran.sfdc14 

Removing multi Spaces in a Text Area

I have two fields one with Rich text Area and other is long Text area. I have Trigger which removes all the HTML tags of Rich Text Area and stores in Long Text Area. Now my Text is generated with multiple spaces. So I tried with

result = result.replaceAll('\\s+',' ');

result = result.replaceAll('  ',' ');

result = result.replaceAll('  ',' ');

But this did not solve my problem. Is there any other solution to remove multi spaces between my text.

 

 

trigger UpdateSearsDescriptionFieldUpdate on ECS__Product__c (before insert, before update) {   

 

  for(ECS__Product__c pro : Trigger.new){       

  if(pro.ECS__Description__c != null && pro.ECS__Description__c != ''){           

  string result = pro.ECS__Description__c.replaceAll('<br/>',' ');         

    System.debug('pro.ECS__Description__c-----:'+pro.ECS__Description__c);       

      System.debug('result-----:'+result);             result = result.replaceAll('<br />',' ');  

           System.debug('result-----:'+result);             string HTML_TAG_PATTERN = '<.*?>';    

         System.debug('HTML_TAG_PATTERN-----:'+HTML_TAG_PATTERN);    

         pattern myPattern = pattern.compile(HTML_TAG_PATTERN);    

         System.debug('myPattern-----:'+myPattern);          

   matcher myMatcher = myPattern.matcher(result);       

      System.debug('myMatcher-----:'+myMatcher);    

         result = myMatcher.replaceAll('');  

           System.debug('result-----:'+result);    

         result = result.replaceAll('<br>','');    

         System.debug('result-----:'+result);    

         result = result.replaceAll('&lt;','');     

        System.debug('result-----:'+result);    

         result = result.replaceAll('&gt;','');     

        result = result.replaceAll('&amp;','&');    

         result = result.replaceAll('&#39;','\'');    

         result = result.replaceAll('\\s+',' ');    

         System.debug('result before replace-----:'+result);     

        result = result.replaceAll('  ',' ');     

        System.debug('result-----:'+result);    

         result = result.replaceAll('&nbsp;&nbsp;',' ');    

         System.debug('result-----:'+result);   

          if(result.length() > 2400) {   

           result = result.substring(0, 2400);   

          }     

        pro.Sears_com_Description__c = result;  

           System.debug('pro.Sears_com_Description__c-----:'+pro.Sears_com_Description__c);    

     }    

 }

 }

This is my code. Sears_com_Description__c is Long Text area and ECS__Description__c  is Rich Text Area.

 

 

Thanks,