Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a built-in function to repeat a string or char in .NET?

Tags:

c#

asp.net

vb.net

Is there a function in C# that returns x times of a given char or string? Or must I code it myself?

like image 388
HasanG Avatar asked Nov 06 '10 20:11

HasanG


People also ask

How do you repeat a character in a string in C#?

The Enumerable. Repeat() function of LINQ can be used to repeat a string to a specified number of times in C#. The Enumerable. Repeat() function takes two parameters, a string variable and the number of times that string variable must be repeated.

How do you make a string repeating characters?

Java has a repeat function to build copies of a source string: String newString = "a". repeat(N); assertEquals(EXPECTED_STRING, newString);


1 Answers

string.Join("", Enumerable.Repeat("ab", 2)); 

Returns

"abab" 

And

string.Join("", Enumerable.Repeat('a', 2)) 

Returns

"aa" 
like image 74
Kirk Woll Avatar answered Sep 21 '22 21:09

Kirk Woll