2

Does apex include any programming idioms to quickly and clearly initialise or pad strings with a specific character, for example 'string multiplication' such as '-'*40 to easily create a rule, or '.'*(size-str.length() to pad to length?

I new to apex, though experienced developer; I want to produce a quick and simple bar graph of a specific dataset to highlight any anomalies. This code will render a bar-graph to the logs, seems very 'old school', surely there is better way to do this in apex? I want something that is clear and concise. Surely there is a way to do without the nested loop in the produce bar graph part of the code?

// retrieve distribution data
Integer[] distribution = new Integer[100];
for(Integer i=0; i<1000 ; i++) {
    // the real data actual comes from elsewhere;
    // this just simulates it with random numbers for this example.
    Integer num = (math.random() * 100).intValue();
    System.assert(num >= 0 && num < 100, 'Value out of expected range : ' + num);
    if (distribution[num] == null) {
        distribution[num] = 1;
    } else {
        distribution[num]++;
    }
}

// produce bar graph
String barGraph = '\n';
for(Integer num=0; num<100 ; num++) {
    barGraph += '' + num + ') ';
    // Surely there is better way to do this bit?
    for(integer i=0 ; i<distribution[num] ; i++) { barGraph += '.'; }
    barGraph += '\n';
}
System.Debug( barGraph );
  • 3
    can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – kurunve Jan 11 at 10:36
  • 2
    So you're rendering a bar graph in the debug log, eh? Is this data ever displayed on a Visualforce page / Lightning component? If so, you may consider using the charting built in to Visualforce or a javascript library like D3.js or chart.js. – Derek F Jan 11 at 13:56
  • @DerekF in this case the code will be used in testing only, it is not part of the core functional requirements, so the test logs will be fine for this output. If you want to suggest wider solution for others to benefit go right ahead. – Martin of Hessle Jan 11 at 14:02
4

You can use String.Repeat

String.repeat Returns the current String repeated the specified number of times.

String s1 = 'SFDC';
String s2 = 
   s1.repeat(2);
System.assertEquals(
   'SFDCSFDC', s2);

Also if you want a delimiter in between you can also specify that.

String s1 = 'SFDC';
String s2 = 
   s1.repeat('-', 2);
System.assertEquals(
   'SFDC-SFDC', s2);

Src : https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_string.htm#apex_System_String_repeat

5

Using the comment by @kurunve for direction, and for the benefit of others, this is the improved code using rightPad.

// single line rule.
System.Debug( ''.rightPad(40, '-' ));
// produce bar graph
String barGraph = '\n';
for(Integer num=0; num<100 ; num++) {
    barGraph += '' + num + ') '.rightPad(distribution[num], '+') + '\n';
}
System.Debug( barGraph );
// produce double line rule.
System.Debug( ''.rightPad(40, '=' ));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.