views:

106

answers:

5

I know I can append to a string but I want to be able to add a specific character after every 5 characters within the string

from this string alpha = abcdefghijklmnopqrstuvwxyz

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

+6  A: 

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

Strings are IEnumerable so you should be able to run a for loop over 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
Preet Sangha
A: 
string[] lines = Regex.Split(value, ".{5}");  
string out = "";
foreach (string line in lines)  
{  
    out += "-" + line;
}
out = out.Substring(1);
Darth Android
You could use `Regex.Replace`, or `String.Join`, and why are you using `\d`?
Kobi
A lot of allocations, not so effective.
Dmitry Karpezo
A: 
string alpha = "abcdefghijklmnopqrstuvwxyz";
string newAlpha = "";
for (int i = 5; i < alpha.Length; i += 6)
{
  newAlpha = alpha.Insert(i, "-");
  alpha = newAlpha;
}
Andrei Bularca
A: 

You may 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, "-");
Eugene Cheverda
+4  A: 

Here is my solution, 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, "-"));
danijels
Why don't you use the String.Insert() function ?
Thibault Falise
+1, Simple and generic solution.
Karthik
@Thibault: I've changed to string.Insert now. I guess I'm too much of a list guy... :)
danijels
Err, but after an inset, the length changes, so `i += position` is wrong. Doesn't it?
Kobi
@Kobi: the code still works, try it out
danijels
Your function doesn't produce a correct result : your for index increment should be `i += (position + character.Length)` as inserting the `character` string shifts the indexes in the string.
Thibault Falise
I just tried - it results in `12345-1234-1234`. Sorry, but I was right `:|`
Kobi
@Thibault: thanks, I was actually just looking into this myself. Edited.
danijels
@Kobi: you were. It is now fixed.
danijels
Another problem with this: it gives O(n^2) performance since you're creating a new string instance (and copying the entire string) every time you call Insert. You need to use a StringBuilder instead (which also supports Insert.)
jammycakes
@jammy: you are also right. Edited.
danijels