.net C#在字符串中添加字符

.net C#在字符串中添加字符,.net,string,visual-studio-2008,char,c#-3.0,.net,String,Visual Studio 2008,Char,C# 3.0,我知道我可以附加到字符串,但我希望能够在字符串中每5个字符后添加一个特定字符 由此 字符串alpha=abcdefghijklmnopqrstuvxyz 对此 string alpha=abcde-fghij-klmno-pqrst-uvwxy-z记住字符串是不可变的,因此需要创建新字符串 字符串是IEnumerable的,因此您应该能够在其上运行for循环 using System; using System.Collections.Generic; using System.Linq; us

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

由此 字符串alpha=abcdefghijklmnopqrstuvxyz

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

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

字符串是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
string alpha=“abcdefghijklmnopqrstuvxyz”;
字符串newAlpha=“”;
对于(int i=5;i
您可以定义此扩展方法:

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

这是我的解决方案,不要过度

    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, "-"));
private静态字符串AppendAtPosition(字符串baseString、int位置、字符串字符)
{
var sb=新的StringBuilder(基线);
for(int i=位置;i
每隔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;
}
publicstringbreakemailid(stringemailid){
string returnVal=string.Empty;
如果(emailId.Length>8){
对于(int i=0;i
我不得不做一些类似的事情,试图通过添加
将一串数字转换成一个时间跨度。基本上我是用2359999,需要把它转换成23:59:59.999。对我来说,这很容易,因为我知道我需要在哪里“插入”这些字符

ts = ts.Insert(6,".");
ts = ts.Insert(4,":");
ts = ts.Insert(2,":");
基本上,使用插入的字符将ts重新分配给自身。我从头到尾地工作,因为我很懒,不想为其他插入的字符做额外的数学运算

ts = ts.Insert(6,".");
ts = ts.Insert(4,":");
ts = ts.Insert(2,":");
您可以通过执行以下操作来尝试类似的操作:

alpha = alpha.Insert(5,"-");
alpha = alpha.Insert(11,"-"); //add 1 to account for 1 -
alpha = alpha.Insert(17,"-"); //add 2 to account for 2 -
...
您可以使用以下选项:

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

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

适用于任何字符串。和往常一样,大小无关紧要

不能附加到字符串,也不能向字符串添加特定字符。无法修改字符串。可以基于现有字符串创建新字符串。这似乎是一个细微的区别,但它可能很重要。你可以使用
Regex.Replace
,或
String.Join
,为什么要使用
\d
?很多分配,不是很有效。为什么不使用String.Insert()函数?@Thibault:我已经改为String.Insert。我想我太喜欢列表了……:)错误,但插入后,长度会发生变化,因此
i+=position
是错误的。不是吗?函数没有生成正确的结果:插入
字符时,for索引增量应该是
i+=(position+character.Length)
,因为插入
字符会移动字符串中的索引。这方面的另一个问题是:创建新字符串实例时,它会提供O(n^2)性能每次调用Insert时(并复制整个字符串)。您需要改用StringBuilder(它也支持Insert)
alpha = alpha.Insert(5,"-");
alpha = alpha.Insert(11,"-"); //add 1 to account for 1 -
alpha = alpha.Insert(17,"-"); //add 2 to account for 2 -
...
string alpha = "abcdefghijklmnopqrstuvwxyz";
int length = alpha.Length;

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