/    Sign up×
Community /Pin to ProfileBookmark

Is there a function to pad a string like this?

Say I want to pad a string with X number of a certain string. So if I wanted to create a var with “hello” entered 3 times, is there an inherent way to do this? So the string would be “hellohellohello”

I’m just wondering if there’s a padding method rather than constant concatenation within a loop.

Thanks in advance.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@pyroDec 21.2003 — How about [URL=http://us3.php.net/manual/en/function.str-pad.php]str_pad()[/URL].
Copy linkTweet thisAlerts:
@mityaauthorDec 21.2003 — lol I felt just as dumb when someone else pointed this out on another forum.

New years resolution: do some research. Cheers.
Copy linkTweet thisAlerts:
@Orsen_CartSep 02.2004 — Hi

Is there an equivalent function in javascript?

I've looked on a few javascript sites in my O'Reilly books and can't find anything.

Many thanks
Copy linkTweet thisAlerts:
@JonaSep 02.2004 — [i]Originally posted by Orsen Cart [/i]

[B]Is there an equivalent function in javascript? [/B][/QUOTE]


<i>
</i> &lt;script type="text/javascript"&gt;
function str_pad(str,pL,pR,lTimes,rTimes){
var p_L = '', p_R = '';
for(i=0; i&lt;lTimes; i++){
p_L += pL;
}
for(i=0; i&lt;rTimes; i++){
p_R += pR;
}
str = p_L+str+p_R;
return str;
}

window.onload = function(){
var str = "my String";
alert(str);
str = str_pad(str,"&lt;","&gt;",3,3);
alert(str);
}
&lt;/script&gt;


[font=arial][size=1][i]Edited for code update.[/i][/size][/font]
×

Success!

Help @mitya spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...