String multiplication idiom in apex?












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 );









share|improve this question


















  • 2





    can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

    – kurunve
    16 hours ago






  • 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
    12 hours ago











  • @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
    12 hours ago
















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 );









share|improve this question


















  • 2





    can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

    – kurunve
    16 hours ago






  • 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
    12 hours ago











  • @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
    12 hours ago














2












2








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 );









share|improve this question














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 );






apex






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 16 hours ago









Martin of HessleMartin of Hessle

836




836








  • 2





    can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

    – kurunve
    16 hours ago






  • 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
    12 hours ago











  • @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
    12 hours ago














  • 2





    can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

    – kurunve
    16 hours ago






  • 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
    12 hours ago











  • @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
    12 hours ago








2




2





can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

– kurunve
16 hours ago





can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

– kurunve
16 hours ago




2




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
12 hours ago





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
12 hours ago













@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
12 hours ago





@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
12 hours ago










2 Answers
2






active

oldest

votes


















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, '=' ));





share|improve this answer





















  • 2





    Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

    – Pranay Jaiswal
    14 hours ago






  • 3





    @pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.

    – Adrian Larson
    13 hours ago






  • 1





    @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.

    – Martin of Hessle
    12 hours ago













  • @MartinofHessle Which you now can. :)

    – Adrian Larson
    9 hours ago



















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






share|improve this answer























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "459"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f246322%2fstring-multiplication-idiom-in-apex%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    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, '=' ));





    share|improve this answer





















    • 2





      Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

      – Pranay Jaiswal
      14 hours ago






    • 3





      @pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.

      – Adrian Larson
      13 hours ago






    • 1





      @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.

      – Martin of Hessle
      12 hours ago













    • @MartinofHessle Which you now can. :)

      – Adrian Larson
      9 hours ago
















    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, '=' ));





    share|improve this answer





















    • 2





      Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

      – Pranay Jaiswal
      14 hours ago






    • 3





      @pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.

      – Adrian Larson
      13 hours ago






    • 1





      @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.

      – Martin of Hessle
      12 hours ago













    • @MartinofHessle Which you now can. :)

      – Adrian Larson
      9 hours ago














    5












    5








    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, '=' ));





    share|improve this answer















    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, '=' ));






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 12 hours ago

























    answered 15 hours ago









    Martin of HessleMartin of Hessle

    836




    836








    • 2





      Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

      – Pranay Jaiswal
      14 hours ago






    • 3





      @pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.

      – Adrian Larson
      13 hours ago






    • 1





      @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.

      – Martin of Hessle
      12 hours ago













    • @MartinofHessle Which you now can. :)

      – Adrian Larson
      9 hours ago














    • 2





      Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

      – Pranay Jaiswal
      14 hours ago






    • 3





      @pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.

      – Adrian Larson
      13 hours ago






    • 1





      @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.

      – Martin of Hessle
      12 hours ago













    • @MartinofHessle Which you now can. :)

      – Adrian Larson
      9 hours ago








    2




    2





    Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

    – Pranay Jaiswal
    14 hours ago





    Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…

    – Pranay Jaiswal
    14 hours ago




    3




    3





    @pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.

    – Adrian Larson
    13 hours ago





    @pranay just add that as an answer. Much more correct for this use case and what I would have answered. But since you already mentioned it.

    – Adrian Larson
    13 hours ago




    1




    1





    @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.

    – Martin of Hessle
    12 hours ago







    @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.

    – Martin of Hessle
    12 hours ago















    @MartinofHessle Which you now can. :)

    – Adrian Larson
    9 hours ago





    @MartinofHessle Which you now can. :)

    – Adrian Larson
    9 hours ago













    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






    share|improve this answer




























      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






      share|improve this answer


























        4












        4








        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






        share|improve this answer













        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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 12 hours ago









        Pranay JaiswalPranay Jaiswal

        14.2k32452




        14.2k32452






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Salesforce Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f246322%2fstring-multiplication-idiom-in-apex%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to make a Squid Proxy server?

            Is this a new Fibonacci Identity?

            19世紀