digitalmars.D.learn - How to catenate a string multiple times ?
- Peter Sommerfeld (6/6) Mar 16 2013 Cannot find a reference: What is the best way
- 1100110 (3/9) Mar 16 2013 foreach(0..4)
- 1100110 (5/15) Mar 16 2013 or
- bearophile (9/12) Mar 16 2013 import std.array;
- John Colvin (33/39) Mar 16 2013 There are very many different ways of doing this, here are a few.
- John Colvin (4/46) Mar 16 2013 or, as bearophile points out, use std.array.replicate
- H. S. Teoh (21/27) Mar 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
Mar 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; // -> "........" Peterforeach(0..4) write("...");
Mar 16 2013
On 03/16/2013 10:39 AM, 1100110 wrote:On 03/16/2013 10:29 AM, Peter Sommerfeld wrote:foreach(0..4)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; // -> "........" Peterwrite("...");or str = str ~ "..."; I shoulda proofread that.
Mar 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
Mar 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; // -> "........" PeterThere 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
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:or, as bearophile points out, use std.array.replicate I should really read over phobos again, I keep forgetting functions exist...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; // -> "........" PeterThere 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
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