>> i = 3 >>> 'hello' * i 'he...">

String Multiplication in C #

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 in Python? I could easily do this in a for loop, but it is tedious and not expressive.

Ultimately, I write to let the console recursively, with the indentation level increasing with every call.

 parent child child child grandchild 

And it would be easiest to do "\t" * indent .

+4
source share
12 answers

It has an extension method 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); 
+19
source

If you need only one character, you can do:

 new string('\t', i) 

See this post for more details.

+12
source

There is nothing built-in in BCL for this, but a little LINQ can complete the task quite easily:

 var multiplied = string.Join("", Enumerable.Repeat("hello", 5).ToArray()); 
+11
source

This is how I do it ...

 string value = new string(' ',5).Replace(" ","Apple"); 
+11
source
 int indent = 5; string s = new string('\t', indent); 
+1
source

One way to do this is as follows: but it is not so nice.

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

UPDATE

Ahhhh ... I remember ... for the characters ...

  new String('x', 3) 
+1
source

what about linq aggregate ...

 var combined = Enumerable.Repeat("hello", 5).Aggregate("", (agg, current) => agg + current); 
+1
source

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

0
source

In mm:

 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(); } 
0
source

While this is only the one character you want to repeat, there is a String constructor that you can use:

 string indentation = new String('\t', indent); 
0
source

I do not think you can extend System.String with operator overloading, but you can make a string wrapper class to do this.

 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 should be for your function to track and skip the current depths and dumps in a for loop based on this.

0
source

If you need a string 3 times, just

 string x = "hello"; string combined = x + x + x; 
-1
source

Source: https://habr.com/ru/post/1285830/


All Articles