6

Possible Duplicate:
Can I "multiply" a string (in C#)?

In Python I can do this:

>>> i = 3
>>> 'hello' * i
'hellohellohello'

How can I multiply strings in C# similarly to in Python? I could easily do it in a for loop but that gets tedious and non-expressive.

Ultimately I'm writing out to console recursively with an indented level being incremented with each call.

parent
    child
    child
    child
        grandchild

And it'd be easiest to just do "\t" * indent.

Community
  • 1
  • 1
Colin Burnett
  • 11,150
  • 6
  • 31
  • 40

12 Answers12

19

There is an extension method for it in this post.

public static string Multiply(this string source, int multiplier)
{
   StringBuilder sb = new StringBuilder(multiplier * source.Length);
   for (int i = 0; i < multiplier; i++)
   {
       sb.Append(source);
   }

   return sb.ToString();
}

string s = "</li></ul>".Multiply(10);
Community
  • 1
  • 1
Shane Fulmer
  • 7,510
  • 6
  • 35
  • 43
14

If you just need a single character you can do:

new string('\t', i)

See this post for more info.

Community
  • 1
  • 1
Chris Van Opstal
  • 36,423
  • 9
  • 73
  • 90
  • This works for my immediate problem so I guess it's the accepted answer. In general though this wouldn't work. :) – Colin Burnett Jun 05 '09 at 20:27
  • I really think the extension method Shane Fulmer posted is a better solution. I'd mark his response as the answer IMHO. – Chris Van Opstal Jun 05 '09 at 20:30
  • @Chris, how do I decide then between the better answer for my specific problem that led to this question and a better, more general answer? – Colin Burnett Jun 05 '09 at 20:32
  • I think Shane Fulmer's answer is a more elegant solution to your specific problem. After you implement it just do "\t".Multiply(i). Not saying I don't appreciate the points :), just think his solution is superior. – Chris Van Opstal Jun 05 '09 at 20:41
12

Here's how I do it...

string value = new string(' ',5).Replace(" ","Apple");
Zachary
  • 6,522
  • 22
  • 34
11

There's nothing built-in to the BCL to do this, but a bit of LINQ can accomplish the task easily enough:

var multiplied = string.Join("", Enumerable.Repeat("hello", 5).ToArray());
Noldorin
  • 144,213
  • 56
  • 264
  • 302
1
int indent = 5;
string s = new string('\t', indent);
Larsenal
  • 49,878
  • 43
  • 152
  • 220
1

One way of doing this is the following - but it's not that nice.

 String.Join(String.Empty, Enumerable.Repeat("hello", 3).ToArray())

UPDATE

Ahhhh ... I remeber ... for chars ...

 new String('x', 3)
Daniel Brückner
  • 59,031
  • 16
  • 99
  • 143
1

how about with a linq aggregate...

var combined = Enumerable.Repeat("hello", 5).Aggregate("", (agg, current) => agg + current);
MSeifert
  • 145,886
  • 38
  • 333
  • 352
Scott Ivey
  • 40,768
  • 21
  • 80
  • 118
  • var combined = Enumerable.Repeat("hello", 5).Aggregate(new StringBuilder(), (agg, current) => agg.Append(current)).ToString(); – sehe Mar 21 '11 at 10:28
0

There is no such statement in C#; your best bet is probably your own MultiplyString() function.

Aric TenEyck
  • 8,002
  • 1
  • 34
  • 48
0

Per mmyers:

public static string times(this string str, int count)
{
  StringBuilder sb = new StringBuilder();
  for(int i=0; i<count; i++) 
  {
    sb.Append(str);
  }
  return sb.ToString();
}
John Weldon
  • 39,849
  • 11
  • 94
  • 127
0

As long as it's only one character that you want to repeat, there is a String constructor that you can use:

string indentation = new String('\t', indent);
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

I don't think that you can extend System.String with an operator overload, but you could make a string wrapper class to do it.

public class StringWrapper
{
    public string Value { get; set; }

    public StringWrapper()
    {
        this.Value = string.Empty;
    }

    public StringWrapper(string value)
    {
        this.Value = value;
    }

    public static StringWrapper operator *(StringWrapper wrapper,
                                           int timesToRepeat)
    {
        StringBuilder builder = new StringBuilder();

        for (int i = 0; i < timesToRepeat; i++)
        {
            builder.Append(wrapper.Value);
        }

        return new StringWrapper(builder.ToString());
    }
}

Then call it like...

var helloTimesThree = new StringWrapper("hello") * 3;

And get the value from...

helloTimesThree.Value;

Of course, the sane thing to do would be to have your function track and pass in the current depth and dump tabs out in a for loop based off of that.

48klocs
  • 6,073
  • 3
  • 27
  • 34
-1

if u need string 3 times just do

string x = "hello";

string combined = x + x + x;
Lloyd Powell
  • 18,270
  • 17
  • 87
  • 123