String multiplication idiom in apex?












1














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


















  • 1




    can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – kurunve
    3 hours ago






  • 1




    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
    13 mins 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
    7 mins ago
















1














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


















  • 1




    can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – kurunve
    3 hours ago






  • 1




    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
    13 mins 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
    7 mins ago














1












1








1







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









Martin of HessleMartin of Hessle

586




586








  • 1




    can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – kurunve
    3 hours ago






  • 1




    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
    13 mins 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
    7 mins ago














  • 1




    can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – kurunve
    3 hours ago






  • 1




    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
    13 mins 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
    7 mins ago








1




1




can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– kurunve
3 hours ago




can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– kurunve
3 hours ago




1




1




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
13 mins 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
13 mins 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
7 mins 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
7 mins ago










1 Answer
1






active

oldest

votes


















3














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
    1 hour ago






  • 2




    @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
    1 hour ago










  • @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
    – Martin of Hessle
    11 mins ago













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

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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
    1 hour ago






  • 2




    @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
    1 hour ago










  • @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
    – Martin of Hessle
    11 mins ago


















3














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
    1 hour ago






  • 2




    @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
    1 hour ago










  • @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
    – Martin of Hessle
    11 mins ago
















3












3








3






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 13 mins ago

























answered 2 hours ago









Martin of HessleMartin of Hessle

586




586








  • 2




    Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – Pranay Jaiswal
    1 hour ago






  • 2




    @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
    1 hour ago










  • @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
    – Martin of Hessle
    11 mins ago
















  • 2




    Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
    – Pranay Jaiswal
    1 hour ago






  • 2




    @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
    1 hour ago










  • @PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
    – Martin of Hessle
    11 mins ago










2




2




Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
1 hour ago




Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
1 hour ago




2




2




@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
1 hour 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
1 hour ago












@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
11 mins ago






@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
11 mins ago




















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

Ronny Ackermann

Köttigit

MySQL 8.0.15 starts normally but any connection hangs