www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to catenate a string multiple times ?

reply "Peter Sommerfeld" <noreply rubrica.at> writes:
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
Mar 16 2013
next sibling parent reply 1100110 <0b1100110 gmail.com> writes:
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("...");
Mar 16 2013
parent 1100110 <0b1100110 gmail.com> writes:
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.
Mar 16 2013
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
Mar 16 2013
prev sibling next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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.
Mar 16 2013
parent "John Colvin" <john.loughran.colvin gmail.com> writes:
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...
Mar 16 2013
prev sibling parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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??
Mar 16 2013