patterncsharpCritical
Is there an easy way to return a string repeated X number of times?
Viewed 0 times
timesreturnrepeatedwaynumberstringeasythere
Problem
I'm trying to insert a certain number of indentations before a string based on an items depth and I'm wondering if there is a way to return a string repeated X times. Example:
string indent = "---";
Console.WriteLine(indent.Repeat(0)); //would print nothing.
Console.WriteLine(indent.Repeat(1)); //would print "---".
Console.WriteLine(indent.Repeat(2)); //would print "------".
Console.WriteLine(indent.Repeat(3)); //would print "---------".Solution
If you only intend to repeat the same character you can use the string constructor that accepts a char and the number of times to repeat it
For example, to repeat a dash five times:
new String(char c, int count).For example, to repeat a dash five times:
string result = new String('-', 5);
Output: -----Code Snippets
string result = new String('-', 5);
Output: -----Context
Stack Overflow Q#3754582, score: 811
Revisions (0)
No revisions yet.