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
SFDC New learnerSFDC New learner 

How to transform a given string pattern?

Hi All,

  I am trying to transform a string pattern lets say 'aaabbbcccbbsc' to a3b3c3b2s1c1.

Below is the code.
public class StringPracticeduplicatecount {
    public  void stringpatterns(String s)
        {
            
              string newstr;
                 list<String> liststr=s.split('');
            system.debug('::'+liststr);
            integer count = 1,i=1;

            for (integer j = 0; j < s.length(); j++)
            {
                if (i<s.length() && liststr[i] == liststr[i - 1])
                {
                    count++;
                }
                else
                {
                    newstr = newstr + liststr[i - 1] + count;
                    count = 1;
                }
                i++;
            }

            system.debug( 'output:  ' + newstr);
        }

}
The code is working fine but when i open the debug log it is showing 'List Size is too large to display'.How to avoid this error?
And also I want to  know if there is any other approach to get the solution.
Any help is greatly appreciated.
Thanks,
Sirisha
 
Sridhar NarayansaSridhar Narayansa
When I tested your code with the string provided, it looks fine.
Looks like it giving the correct response. see image.

The error you are getting is obvious. The string that is being passed is too big for System.Debug for printing. Honestly printing that list will not make any sense because you are printing every character in the string as a list. Instead print the string the incoming string and make sense out it.

User-added image