5

Just a curiosity I was investigating.

The matter: simply repeating (multiplying, someone would say) a string/character n times.
I know there is Enumerable.Repeat for this aim, but I was trying to do this without it. LINQ in this case seems pretty useless, because in a query like

from X in "s" select X  

the string "s" is being explored and so X is a char. The same is with extension methods, because for example "s".Aggregate(blablabla) would again work on just the character 's', not the string itself. For repeating the string something "external" would be needed, so I thought lambdas and delegates, but it can't be done without declaring a variable to assign the delegate/lambda expression to.
So something like defining a function and calling it inline:

( (a)=>{return " "+a;} )("a");  

or

delegate(string a){return " "+a}(" ");  

would give a "without name" error (and so no recursion, AFAIK, even by passing a possible lambda/delegate as a parameter), and in the end couldn't even be created by C# because of its limitations.
It could be that I'm watching this thing from the wrong perspective. Any ideas?
This is just an experiment, I don't care about performances, about memory use... Just that it is one line and sort of autonomous. Maybe one could do something with Copy/CopyTo, or casting it to some other collection, I don't know. Reflection is accepted too.

lunadir
  • 343
  • 2
  • 12

2 Answers2

9

To repeat a character n-times you would not use Enumerable.Repeat but just this string constructor:

string str = new string('X', 10);

To repeat a string i don't know anything better than using string.Join and Enumerable.Repeat

string foo = "Foo";
string str = string.Join("", Enumerable.Repeat(foo, 10));

edit: you could use string.Concat instead if you need no separator:

string str = string.Concat( Enumerable.Repeat(foo, 10) );
Tim Schmelter
  • 411,418
  • 61
  • 614
  • 859
2

If you're trying to repeat a string, rather than a character, a simple way would be to use the StringBuilder.Insert method, which takes an insertion index and a count for the number of repetitions to use:

var sb = new StringBuilder();
sb.Insert(0, "hi!", 5);
Console.WriteLine(sb.ToString());

Otherwise, to repeat a single character, use the string constructor as I've mentioned in the comments for the similar question here. For example:

string result = new String('-', 5); // -----

For the sake of completeness, it's worth noting that StringBuilder provides an overloaded Append method that can repeat a character, but has no such overload for strings (which is where the Insert method comes in). I would prefer the string constructor to the StringBuilder if that's all I was interested in doing. However, if I was already working with a StringBuilder, it might make sense to use the Append method to benefit from some chaining. Here's a contrived example to demonstrate:

var sb = new StringBuilder("This item is ");
sb.Insert(sb.Length, "very ", 2) // insert at the end to append
  .Append('*', 3)
  .Append("special")
  .Append('*', 3);
Console.WriteLine(sb.ToString()); // This item is very very ***special***
Community
  • 1
  • 1
Ahmad Mageed
  • 88,056
  • 18
  • 152
  • 168