C # adding character to string

I know that I can add to a string, but I want to add a specific character after every 5 characters in a string

from this line alpha = abcdefghijklmnopqrstuvwxyz

to this line alpha = abcde-fghij-klmno-pqrst-uvwxy-z

+6
visual-studio-2008
source share
7 answers

Remember that a string is immutable, so you need to create a new string.

IEnumerable strings, so you should be able to run the for loop through

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 the following:

 Before: abcdefghijklmnopqrstuvwxyz After: abcde-fghij-klmno-pqrst-uvwxy-z 
+15
source share

Here is my decision without overdoing it.

  private static string AppendAtPosition(string baseString, int position, string character) { var sb = new StringBuilder(baseString); for (int i = position; i < sb.Length; i += (position + character.Length)) sb.Insert(i, character); return sb.ToString(); } Console.WriteLine(AppendAtPosition("abcdefghijklmnopqrstuvwxyz", 5, "-")); 
+10
source share
 string alpha = "abcdefghijklmnopqrstuvwxyz"; string newAlpha = ""; for (int i = 5; i < alpha.Length; i += 6) { newAlpha = alpha.Insert(i, "-"); alpha = newAlpha; } 
+3
source share

I had to do something similar, trying to convert a string of numbers to a period of time by adding in : and . . I basically took 235959999 and had to convert it to 23: 59: 59.999. It was easy for me, because I knew where I needed to "insert" the specified characters.

 ts = ts.Insert(6,"."); ts = ts.Insert(4,":"); ts = ts.Insert(2,":"); 

Basically reassigning ts to itself with a character inserted. I made my way from the back to the front because I was lazy and did not want to do additional math for the other characters inserted.

You can try something like this by doing:

 alpha = alpha.Insert(5,"-"); alpha = alpha.Insert(11,"-"); //add 1 to account for 1 - alpha = alpha.Insert(17,"-"); //add 2 to account for 2 - ... 
+2
source share

Insert space in emailId field after every 8 characters

 public string BreakEmailId(string emailId) { string returnVal = string.Empty; if (emailId.Length > 8) { for (int i = 0; i < emailId.Length; i += 8) { returnVal += emailId.Substring(i, 8) + " "; } } return returnVal; } 
+1
source share
 string[] lines = Regex.Split(value, ".{5}"); string out = ""; foreach (string line in lines) { out += "-" + line; } out = out.Substring(1); 
0
source share

You can define this extension method:

 public static class StringExtenstions { public static string InsertCharAtDividedPosition(this string str, int count, string character) { var i = 0; while (++i * count + (i - 1) < str.Length) { str = str.Insert((i * count + (i - 1)), character); } return str; } } 

And use it like:

 var str = "abcdefghijklmnopqrstuvwxyz"; str = str.InsertCharAtDividedPosition(5, "-"); 
0
source share

All Articles