pad a string

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

pad a string

Chris Datfung
Hi,

I want to pad a string with itself n number of times, for example if n = 3 and var="test" then var = var .. var .. var

I tried with a for loop but get one too many combinations

> a="k"
> n=3
> for c = 1, n-1 do a = a..a end
> print(a)
kkkk

Is there a smarter way to implement this?

Thanks,
Chris
Reply | Threaded
Open this post in threaded view
|

Re: pad a string

Choonster TheMage
On Thu, Apr 18, 2013 at 10:15 PM, Chris Datfung <[hidden email]> wrote:

> Hi,
>
> I want to pad a string with itself n number of times, for example if n = 3
> and var="test" then var = var .. var .. var
>
> I tried with a for loop but get one too many combinations
>
>> a="k"
>> n=3
>> for c = 1, n-1 do a = a..a end
>> print(a)
> kkkk
>
> Is there a smarter way to implement this?
>
> Thanks,
> Chris

You want the string.rep function:
http://www.lua.org/manual/5.1/manual.html#pdf-string.rep
http://www.lua.org/manual/5.2/manual.html#pdf-string.rep

Reply | Threaded
Open this post in threaded view
|

RE: pad a string

Chodera, Ian
In reply to this post by Chris Datfung

a="k"

n=3

b=""

for c = 1, n do b = b..a end

print(b)

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Chris Datfung
Sent: 18 April 2013 13:16
To: Lua mailing list
Subject: pad a string

 

Hi,

I want to pad a string with itself n number of times, for example if n = 3 and var="test" then var = var .. var .. var

I tried with a for loop but get one too many combinations

> a="k"
> n=3
> for c = 1, n-1 do a = a..a end
> print(a)
kkkk

Is there a smarter way to implement this?

Thanks,

Chris

**************************************************************************************** Note: If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by replying to the message and deleting it from your computer. Thank you. ****************************************************************************************
Reply | Threaded
Open this post in threaded view
|

Re: pad a string

Kaj Eijlers
You are unintentionally doubling a every time - instead of appending your original, you append the modified string to itself. so you are doing
a = 'a'
a = a + a = 'a' + 'a' -> a is now 'aa'
a = a + a = 'aa' + 'aa' -> a is now 'aaaa'
a = a + a = 'aaaa' + 'aaaa' -> a is now 'aaaaaaaa'
etc.

Kaj



On Thu, Apr 18, 2013 at 8:19 AM, Chodera, Ian <[hidden email]> wrote:

a="k"

n=3

b=""

for c = 1, n do b = b..a end

print(b)

 

From: [hidden email] [mailto:[hidden email]] On Behalf Of Chris Datfung
Sent: 18 April 2013 13:16
To: Lua mailing list
Subject: pad a string

 

Hi,

I want to pad a string with itself n number of times, for example if n = 3 and var="test" then var = var .. var .. var

I tried with a for loop but get one too many combinations

> a="k"
> n=3
> for c = 1, n-1 do a = a..a end
> print(a)
kkkk

Is there a smarter way to implement this?

Thanks,

Chris

**************************************************************************************** Note: If the reader of this message is not the intended recipient, or an employee or agent responsible for delivering this message to the intended recipient, you are hereby notified that any dissemination, distribution or copying of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by replying to the message and deleting it from your computer. Thank you. ****************************************************************************************

Reply | Threaded
Open this post in threaded view
|

Re: pad a string

Craig Barnes
In reply to this post by Chris Datfung
On 18 April 2013 13:15, Chris Datfung <[hidden email]> wrote:

>
> Hi,
>
> I want to pad a string with itself n number of times, for example if n = 3 and var="test" then var = var .. var .. var
>
> I tried with a for loop but get one too many combinations
>
> > a="k"
> > n=3
> > for c = 1, n-1 do a = a..a end
> > print(a)
> kkkk
>
> Is there a smarter way to implement this?
>

print(string.rep("k", 3))

Reply | Threaded
Open this post in threaded view
|

Re: pad a string

Chris Datfung
On Thu, Apr 18, 2013 at 4:27 PM, Craig Barnes <[hidden email]> wrote:
On 18 April <a href="tel:2013" value="+9722013">2013 13:15, Chris Datfung <[hidden email]> wrote:
>
> Hi,
>
> I want to pad a string with itself n number of times, for example if n = 3 and var="test" then var = var .. var .. var
>
> I tried with a for loop but get one too many combinations
>
> > a="k"
> > n=3
> > for c = 1, n-1 do a = a..a end
> > print(a)
> kkkk
>
> Is there a smarter way to implement this?
>

print(string.rep("k", 3))


Thanks!