.net C#在字符串中添加一个字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3879710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# adding a character in a string
提问by pablosal
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
我知道我可以附加到一个字符串,但我希望能够在字符串中的每 5 个字符后添加一个特定字符
from this string alpha = abcdefghijklmnopqrstuvwxyz
从这个字符串 alpha = abcdefghijklmnopqrstuvwxyz
to this string alpha = abcde-fghij-klmno-pqrst-uvwxy-z
到这个字符串 alpha = abcde-fghij-klmno-pqrst-uvwxy-z
回答by Preet Sangha
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
字符串是 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);
}
}
}
Produces this:
产生这个:
Before: abcdefghijklmnopqrstuvwxyz
After: abcde-fghij-klmno-pqrst-uvwxy-z
回答by NocFenix
I had to do something similar, trying to convert a string of numbers into a timespan by adding in :and .. Basically I was taking 235959999 and needing to convert it to 23:59:59.999. For me it was easy because I knew where I needed to "insert" said characters.
我不得不做类似的事情,尝试通过添加:和将一串数字转换为时间跨度.。基本上我正在服用 235959999 并需要将其转换为 23:59:59.999。对我来说这很容易,因为我知道我需要在哪里“插入”所述字符。
ts = ts.Insert(6,".");
ts = ts.Insert(4,":");
ts = ts.Insert(2,":");
Basically reassigning ts to itself with the inserted character. I worked my way from the back to front, because I was lazy and didn't want to do additional math for the other inserted characters.
基本上是用插入的字符将 ts 重新分配给自己。我从后到前工作,因为我很懒,不想为其他插入的字符做额外的数学运算。
You could try something similar 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 -
...
回答by danijels
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, "-"));
回答by Andrei Bularca
string alpha = "abcdefghijklmnopqrstuvwxyz";
string newAlpha = "";
for (int i = 5; i < alpha.Length; i += 6)
{
newAlpha = alpha.Insert(i, "-");
alpha = newAlpha;
}
回答by Raj
Inserting Space in emailId field after every 8 characters
每 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;
}
回答by Eugene Cheverda
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, "-");
回答by Darth Android
string[] lines = Regex.Split(value, ".{5}");
string out = "";
foreach (string line in lines)
{
out += "-" + line;
}
out = out.Substring(1);

