views:

114

answers:

9

I have the following function that takes a string as parameter and repeats it a number of times (also a parameter). I feel like this is something that's already in the framework or at least could be done better. Any suggestions?

private string chr(string s, int repeat)
{
    string result = string.Empty;
    for (int i = 0; i < repeat; i++)
    {
        result += s;
    }
    return result;
}
+1  A: 

You might want to consider using StringBuilder if your repeat parameter is very large.

http://msdn.microsoft.com/en-us/library/system.text.stringbuilder.aspx

http://channel9.msdn.com/forums/TechOff/14294-C-string-vs-StringBuilder/

Wesley Wiser
+4  A: 

I'd use a StringBuilder as currently you're potentially allocating and deallocating lots of strings:

private string chr(string s, int repeat)
{
    StringBuilder result = new StringBuilder();
    for (int i = 0; i < repeat; i++)
    {
        result.Append(s);
    }
    return result.ToString();
}

Or even better if the string is only a single character:

private string chr(char s, int repeat)
{
    StringBuilder result = new StringBuilder();
    result.Append(s, repeat);
    return result.ToString();
}
ChrisF
+4  A: 

Not the most efficient, but concise:

.NET 4:

String.Join(String.Empty, Enumerable.Repeat(s, repeat));

.NET 3.0/3.5:

String.Join(String.Empty, Enumerable.Repeat(s, repeat).ToArray());
Julien Lebosquain
I like this - clever!
Daniel Schaffer
+4  A: 

If your input is really a single character rather than a string, you could simply do this:

var someChar = 'f';
var repeat = 10;
var repeated = new String(someChar, repeat);

Otherwise, I dont' think there's much else to do aside from using StringBuilder instead of concatenation:

private string chr(string s, int repeat)
{
    var result = new StringBuilder(s.Length * repeat);
    for (int i = 0; i < repeat; i++)
    {
        result.Append(s);
    }
    return result.ToString();
}
Daniel Schaffer
why not `result.Append(s, 0, repeat)` instead of the `for` cycle
Imre L
because that overload doesn't exist - the overload `Append(string, int int)` overload parameters are `value`, `startIndex` and `count`, which means it'll be appending a substring of `value`, not appending `value` `count` times.
Daniel Schaffer
+4  A: 
private string chr (string s, int repeat) {
   string result = new String(' ', repeat).Replace(" ",  s);
   return result;
}
diez
This is clever too :D
Daniel Schaffer
I do like this!
FallingBullets
You are far too clever! I don't know if I can use this though - the next guy who maintains the code will probably want to dismember me.
AngryHacker
+1  A: 
private string chr(string s, int repeat)
{
    return Enumerable.Range(0, repeat)
        .Aggregate(new StringBuilder(), (sb, i) => sb.Append(s)).ToString();
}
Lee
+3  A: 

Functional programming-style approach:
(requires at least C# 3.0)

static class StringRepetitionExtension
{
    public static string Times(this int count, string what)
    {
        return count > 0 ? string.Concat(what, (count-1).Times(what))
                         : string.Empty;
    }
}

Usage:

3.Times("Foobar")   // returns "FoobarFoobarFoobar"

(Certainly not the most efficient solution, and due to the recursion there's always the danger of stack overflow with unreasonably large values for count; but I nevertheless wanted to share a slightly different, easy-to-understand approach.)

stakx
This looks like ruby :)
Wesley Wiser
That's flat out awesome.
AngryHacker
Accepted for its awesomeness and elegance, not for its efficiency. For efficiency see Imre L's answer.
AngryHacker
+5  A: 

The most important improvement you could make to your function is to give it a descriptive name.

Daniel Earwicker
+2  A: 
return new System.Text.StringBuilder().Insert(0,"repeatme",count).ToString()
Imre L
I'm surprised this hasn't got any upvotes so far, as it's probably the most straightforward solution for relying on no cleverness apart from using what the BCL already offers. _@Imre L:_ You could add a link to the MSDN documentation page for `StringBuilder.Insert(Int32, String, Int32)`: http://msdn.microsoft.com/en-us/library/62eb5xsf.aspx
stakx