.net - C# adding a character in a string -


i know can append string want able add specific character after every 5 characters within string

from string alpha = abcdefghijklmnopqrstuvwxyz

to string alpha = abcde-fghij-klmno-pqrst-uvwxy-z

remember string immutable need create new string.

strings ienumerable should able run loop on it

using system; using system.collections.generic; using system.linq; using system.text;  namespace consoleapplication1 {     class program     {         static void main(string[] args)         {             string alpha = "abcdefghijklmnopqrstuvwxyz";             var builder = new stringbuilder();             int count = 0;             foreach (var c in alpha)             {                 builder.append(c);                 if ((++count % 5) == 0)                 {                     builder.append('-');                 }             }             console.writeline("before: {0}", alpha);             alpha = builder.tostring();             console.writeline("after: {0}", alpha);         }     } } 

produces this:

before: abcdefghijklmnopqrstuvwxyz after: abcde-fghij-klmno-pqrst-uvwxy-z 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -