There is a string string s = "....." , you need to insert into the string s the character '*' the number of times I specified, for example, s = ".***." .

  • one
    C # strings are immutable. Want efficient access by index - use StringBuilder . - VladD

4 answers 4

 s = s.Substring(0, index) + new string('*', repCount) + s.Substring(index + repCount); // index - индекс, с которого менять; repCount - количество звездочек 
     s = s.Insert(1, new string('*', 5)); 

    And do not need any cycles.

    • one
      Does it bother you that with such an insertion from the string "....." the string ". ***."? The question indicates exactly such a transformation. - kodv
    • one
      @kodv, no, not embarrassed, apparently the author was mistaken with an example, which confirms the accepted answer. The insert is an insert, in the example the author describes a replacement. - ixSci
     s = new Regex(".").Replace(s, "*", 3, 1); 
    • In this case, the pattern "." (one any character). - Romario

    Try s = s.Insert (index, "***");

     string strCount = ""; for (int i = 0; i < count; i++) strCount += "*"; s.Insert(index, strCount); 
    • Firstly, it turns out ".. *** ...". Secondly, I want to drive it into a loop then. To solve a more global problem. Of course, you can remove the last point. But this option does not suit me. I need something something like s = s.Insert (index, "*", 3); if it were possible so naturally. - Faradey Inimicos
    • @FaradeyInimicos Ru.stackoverflow.com/q/443446/179270 - Bald