2010-10-07 36 views
6

我知道我可以追加到一个字符串,但我希望能够为每5个字符后的字符串C#中的字符串

距离这 字符串的α= ABCDEFGHIJKLMNOPQRSTUVWXYZ

添加特定的字符添加字符

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

+1

不能追加到一个字符串,你不能一个特定的字符添加到字符串。字符串不能修改。你*可以*根据现有的字符串创建一个新的字符串。看起来像一个微妙的差异,但它可能很重要。 – 2010-10-07 08:00:44

+1

相关http://stackoverflow.com/questions/3306568/how-do-i-set-a-character-at-an-index-in-a-string-in-c/ – 2010-10-07 08:19:33

回答

15

记住一个字符串是不可变的,所以你需要创建一个新的字符串。

字符串是IEnumerable的,所以你应该能够超过它循环运行

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 
0
string[] lines = Regex.Split(value, ".{5}"); 
string out = ""; 
foreach (string line in lines) 
{ 
    out += "-" + line; 
} 
out = out.Substring(1); 
+0

你可以使用'正则表达式。替换',或'String.Join',为什么你使用'\ d'? – Kobi 2010-10-07 08:11:03

+0

很多分配,不太有效。 – 2010-10-07 08:11:18

3
string alpha = "abcdefghijklmnopqrstuvwxyz"; 
string newAlpha = ""; 
for (int i = 5; i < alpha.Length; i += 6) 
{ 
    newAlpha = alpha.Insert(i, "-"); 
    alpha = newAlpha; 
} 
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, "-"); 
10

这里是我的解决方案,不勉强。在EMAILID场

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

为什么不使用String.Insert()函数? – 2010-10-07 08:43:23

+1

+1,简单而通用的解决方案。 – 2010-10-07 08:46:31

+0

@Thibault:我已经更改为string.Insert现在。我想我是一个名单太多的人...... :) – danijels 2010-10-07 08:52:31

1

插入用空间的每8个字符

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

后,我不得不做类似的事情,试图通过在:.增加数字的字符串转换成时间跨度。基本上我采取235959999,并需要将其转换为23:59:59.999。对我而言,这很容易,因为我知道我需要在哪里插入所说的角色。

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 - 
...