.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
Post a Comment