Thread overview
How to catenate a string multiple times ?
Mar 16, 2013
Peter Sommerfeld
Mar 16, 2013
1100110
Mar 16, 2013
1100110
Mar 16, 2013
bearophile
Mar 16, 2013
John Colvin
Mar 16, 2013
John Colvin
Mar 16, 2013
H. S. Teoh
March 16, 2013
Cannot find a reference: What is the best way
to catenate a string multiple times ?
Unfortunately this this does not work ;-)

string tab = "..";
tab = tab * 4; // -> "........"

Peter
March 16, 2013
On 03/16/2013 10:29 AM, Peter Sommerfeld wrote:
> Cannot find a reference: What is the best way
> to catenate a string multiple times ?
> Unfortunately this this does not work ;-)
>
> string tab = "..";
> tab = tab * 4; // -> "........"
>
> Peter
foreach(0..4)
	write("...");
March 16, 2013
On 03/16/2013 10:39 AM, 1100110 wrote:
> On 03/16/2013 10:29 AM, Peter Sommerfeld wrote:
>> Cannot find a reference: What is the best way
>> to catenate a string multiple times ?
>> Unfortunately this this does not work ;-)
>>
>> string tab = "..";
>> tab = tab * 4; // -> "........"
>>
>> Peter


foreach(0..4)
> write("...");

or
str = str ~ "...";

I shoulda proofread that.
March 16, 2013
Peter Sommerfeld:

> Cannot find a reference: What is the best way
> to catenate a string multiple times ?


import std.array;

void main() {
    string tab = "..";
    tab = tab.replicate(4);
}


> Unfortunately this this does not work ;-)

D has vector ops, so I guess it's better to not add that.

Bye,
bearophile
March 16, 2013
On Saturday, 16 March 2013 at 15:29:03 UTC, Peter Sommerfeld wrote:
> Cannot find a reference: What is the best way
> to catenate a string multiple times ?
> Unfortunately this this does not work ;-)
>
> string tab = "..";
> tab = tab * 4; // -> "........"
>
> Peter

There are very many different ways of doing this, here are a few.

using template recursion:

//Repeats string "s" size_t "num" times at compile-time
template RepeatString (string s, size_t num)
{
    static if(num == 0)
        const RepeatString = "";
    else
        const RepeatString = s ~ RepeatString!(s, num-1);
}


or for fast runtime performance (also works at compile-time), something like this:

//appends any array to itself multiple times.
T[] concat(T)(T[] arr, size_t num_times)
{
    auto app = appender!(T[])(arr);
    app.reserve(num_times);

    foreach(i; 0..num_times)
        app ~= arr;

    return app.data;
}


simpler but slower:

T[] concat_slow(T)(T[] arr, size_t num_times)
{
    auto result = arr;
    foreach(i; 0..num_times)
        result ~= arr;
    return result;
}


If you needed extreme performance there are faster ways, but they are much longer and more complex.
March 16, 2013
On Saturday, 16 March 2013 at 15:58:32 UTC, John Colvin wrote:
> On Saturday, 16 March 2013 at 15:29:03 UTC, Peter Sommerfeld wrote:
>> Cannot find a reference: What is the best way
>> to catenate a string multiple times ?
>> Unfortunately this this does not work ;-)
>>
>> string tab = "..";
>> tab = tab * 4; // -> "........"
>>
>> Peter
>
> There are very many different ways of doing this, here are a few.
>
> using template recursion:
>
> //Repeats string "s" size_t "num" times at compile-time
> template RepeatString (string s, size_t num)
> {
>     static if(num == 0)
>         const RepeatString = "";
>     else
>         const RepeatString = s ~ RepeatString!(s, num-1);
> }
>
>
> or for fast runtime performance (also works at compile-time), something like this:
>
> //appends any array to itself multiple times.
> T[] concat(T)(T[] arr, size_t num_times)
> {
>     auto app = appender!(T[])(arr);
>     app.reserve(num_times);
>
>     foreach(i; 0..num_times)
>         app ~= arr;
>
>     return app.data;
> }
>
>
> simpler but slower:
>
> T[] concat_slow(T)(T[] arr, size_t num_times)
> {
>     auto result = arr;
>     foreach(i; 0..num_times)
>         result ~= arr;
>     return result;
> }
>
>
> If you needed extreme performance there are faster ways, but they are much longer and more complex.

or, as bearophile points out, use std.array.replicate

I should really read over phobos again, I keep forgetting functions exist...
March 16, 2013
On Sat, Mar 16, 2013 at 04:29:02PM +0100, Peter Sommerfeld wrote:
> Cannot find a reference: What is the best way
> to catenate a string multiple times ?
> Unfortunately this this does not work ;-)
> 
> string tab = "..";
> tab = tab * 4; // -> "........"
[...]

You could define something like this:

	string x(string a, int n) {
		import std.array;
		auto app = appender!string(a);
		while (--n) {
			app.put(a);
		}
		return app.data;
	}

	void main() {
		// prints "abcabcabc"
		writeln("abc".x(3));
	}

It's not as nice as "abc"*3, but unfortunately opBinary only works when declared inside the class/struct; UFCS doesn't pick it up, so it doesn't work on native types.


T

-- 
Why do conspiracy theories always come from the same people??