String multiplication idiom in apex?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
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
add a comment |
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
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
add a comment |
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
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
apex
asked Jan 11 at 10:15
Martin of HessleMartin of Hessle
956
956
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
add a comment |
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
3
3
can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– kurunve
Jan 11 at 10:36
can rightPad help you? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– kurunve
Jan 11 at 10:36
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
Jan 11 at 13:56
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
@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
add a comment |
2 Answers
2
active
oldest
votes
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, '=' ));
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
Jan 11 at 12:16
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♦
Jan 11 at 12:52
1
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
Jan 11 at 13:57
@MartinofHessle Which you now can. :)
– Adrian Larson♦
Jan 11 at 17:13
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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, '=' ));
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
Jan 11 at 12:16
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♦
Jan 11 at 12:52
1
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
Jan 11 at 13:57
@MartinofHessle Which you now can. :)
– Adrian Larson♦
Jan 11 at 17:13
add a comment |
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, '=' ));
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
Jan 11 at 12:16
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♦
Jan 11 at 12:52
1
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
Jan 11 at 13:57
@MartinofHessle Which you now can. :)
– Adrian Larson♦
Jan 11 at 17:13
add a comment |
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, '=' ));
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, '=' ));
edited Jan 11 at 13:55
answered Jan 11 at 11:38
Martin of HessleMartin of Hessle
956
956
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
Jan 11 at 12:16
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♦
Jan 11 at 12:52
1
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
Jan 11 at 13:57
@MartinofHessle Which you now can. :)
– Adrian Larson♦
Jan 11 at 17:13
add a comment |
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
Jan 11 at 12:16
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♦
Jan 11 at 12:52
1
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
Jan 11 at 13:57
@MartinofHessle Which you now can. :)
– Adrian Larson♦
Jan 11 at 17:13
2
2
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
Jan 11 at 12:16
Another way would be to use String.repeat . developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
Jan 11 at 12:16
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♦
Jan 11 at 12:52
@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♦
Jan 11 at 12:52
1
1
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
Jan 11 at 13:57
@PranayJaiswal, I'm happy to accept a better answer, if you want to post an answer using that approach.
– Martin of Hessle
Jan 11 at 13:57
@MartinofHessle Which you now can. :)
– Adrian Larson♦
Jan 11 at 17:13
@MartinofHessle Which you now can. :)
– Adrian Larson♦
Jan 11 at 17:13
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jan 11 at 14:24
Pranay JaiswalPranay Jaiswal
18.7k53158
18.7k53158
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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