C#在字符串中添加一个字符

时间:2010-10-07 07:57:21

标签: .net visual-studio-2008 c#-3.0

我知道我可以附加到字符串但我想能够在字符串中每5个字符后添加一个特定的字符

从此开始 string alpha = abcdefghijklmnopqrstuvwxyz

对此 string alpha = abcde-fghij-klmno-pqrst-uvwxy-z

8 个答案:

答案 0 :(得分:18)

记住字符串是不可变的,因此您需要创建一个新字符串。

字符串是IEnumerable所以你应该可以在它上面运行for循环

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);
        }
    }
}

产生这个:

Before: abcdefghijklmnopqrstuvwxyz
After: abcde-fghij-klmno-pqrst-uvwxy-z

答案 1 :(得分:11)

这是我的解决方案,没有过度使用它。

    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, "-"));

答案 2 :(得分:8)

我必须做类似的事情,尝试通过添加:.将一串数字转换为时间跨度。基本上我采取235959999并需要将其转换为23:59:59.999。对我来说这很容易,因为我知道我需要“插入”所说的角色。

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

基本上用插入的字符重新分配ts给自己。我从后到前工作,因为我很懒,不想为其他插入的字符做额外的数学运算。

您可以通过以下方式尝试类似的事情:

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

答案 3 :(得分:4)

string alpha = "abcdefghijklmnopqrstuvwxyz";
string newAlpha = "";
for (int i = 5; i < alpha.Length; i += 6)
{
  newAlpha = alpha.Insert(i, "-");
  alpha = newAlpha;
}

答案 4 :(得分:1)

每8个字符后在emailId字段中插入空格

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;
}

答案 5 :(得分:0)

string[] lines = Regex.Split(value, ".{5}");  
string out = "";
foreach (string line in lines)  
{  
    out += "-" + line;
}
out = out.Substring(1);

答案 6 :(得分:0)

您可以定义此扩展方法:

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;
        }
    }

并使用它:

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

答案 7 :(得分:0)

您可以使用此:

string alpha = "abcdefghijklmnopqrstuvwxyz";
int length = alpha.Length;

for (int i = length - ((length - 1) % 5 + 1); i > 0; i -= 5)
{
    alpha = alpha.Insert(i, "-");
}

与任何字符串完美配合。与往常一样,大小无关紧要。 ;)