There is a word: string str = "text";
How to set 20 repetitions of this word, that is, that the word "text" was written 20 times in a row? "texttexttext..." and so 20 times.

    2 answers 2

    To do this, you can use StringBuilder and the usual for loop from 0 to 19 (inclusive):

     StringBuilder sb = new StringBuilder(); string str = "text"; for (int i = 0; i < 20; i++) { sb.Append(str); } string result = sb.ToString(); 

    There is a shorter option using Enumerable.Repeat and string.Concat :

     string result = string.Concat(Enumerable.Repeat(str, 20)); 
       string txt = "text"; for (int i = 1; i < 20; i++) { txt += "text"; } 
      • I think to change one digit in the cycle is not difficult, and the author of the question gave an example. I think it’s clear how the program will behave. - Emmanuel
      • Corrected the number of iterations. - Emmanuel
      • For the first time, everything worked as it should - exactly 20 iterations. Do not powder my brains, please. - Emmanuel
      • Understood my mistake. - Emmanuel
      • No, let's not chat. The main thing that you understand and correct the error. More relevant comments, by the way, should be removed. - Regent