C# 如何在字符串的每个第n个位置附加连字符?

C# 如何在字符串的每个第n个位置附加连字符?,c#,asp.net,.net,C#,Asp.net,.net,我从另一个应用程序中获得了类似于“123456”,“abcdef”,“123abc”的字符串。 我需要格式化字符串,如“123-456”,“abc def”,“123 abc”。字符串的长度也可以变化,就像我们可以有一个长度为30个字符的字符串一样。如果选择第三个位置,则应在每三个字符后插入连字符 例如:输入“abcdxy123z” 第三位置输出“abc-dxy-123-z” 如果我们选择第二位置,则输出将为 “ab-cd-xy-12-3z” 我试过使用String.Format(“{0:###

我从另一个应用程序中获得了类似于
“123456”
“abcdef”
“123abc”
的字符串。 我需要格式化字符串,如
“123-456”,“abc def”
“123 abc”
。字符串的长度也可以变化,就像我们可以有一个长度为30个字符的字符串一样。如果选择第三个位置,则应在每三个字符后插入连字符

例如:输入“abcdxy123z” 第三位置输出“abc-dxy-123-z

如果我们选择第二位置,则输出将为

“ab-cd-xy-12-3z”


我试过使用String.Format(“{0:###################################转换为int64(“123456789;########

string Hyphenate(string str, int pos) {
    return String.Join("-",
        str.Select((c, i) => new { c, i })
           .GroupBy(x => x.i / pos)
           .Select(g => String.Join("", g.Select(x => x.c))));
}
string Hyphenate(string str, int pos) {
    return String.Join("-",
        Enumerable.Range(0, (str.Length - 1) / pos + 1)
            .Select(i => str.Substring(i * pos, Math.Min(str.Length - i * pos, pos))));
}
string Hyphenate(string str, int pos) {
    return String.Join("-", Regex.Split(str, @"(.{" + pos + "})")
                                 .Where(s => s.Length > 0));
}
string Hyphenate(string str, int pos) {
    return String.Join("-", Regex.Split(str, @"(?<=\G.{" + pos + "})(?!$)"));
}
或者像这样:

string Hyphenate(string str, int pos) {
    return String.Join("-",
        str.Select((c, i) => new { c, i })
           .GroupBy(x => x.i / pos)
           .Select(g => String.Join("", g.Select(x => x.c))));
}
string Hyphenate(string str, int pos) {
    return String.Join("-",
        Enumerable.Range(0, (str.Length - 1) / pos + 1)
            .Select(i => str.Substring(i * pos, Math.Min(str.Length - i * pos, pos))));
}
string Hyphenate(string str, int pos) {
    return String.Join("-", Regex.Split(str, @"(.{" + pos + "})")
                                 .Where(s => s.Length > 0));
}
string Hyphenate(string str, int pos) {
    return String.Join("-", Regex.Split(str, @"(?<=\G.{" + pos + "})(?!$)"));
}
或者可以使用正则表达式,如下所示:

string Hyphenate(string str, int pos) {
    return String.Join("-",
        str.Select((c, i) => new { c, i })
           .GroupBy(x => x.i / pos)
           .Select(g => String.Join("", g.Select(x => x.c))));
}
string Hyphenate(string str, int pos) {
    return String.Join("-",
        Enumerable.Range(0, (str.Length - 1) / pos + 1)
            .Select(i => str.Substring(i * pos, Math.Min(str.Length - i * pos, pos))));
}
string Hyphenate(string str, int pos) {
    return String.Join("-", Regex.Split(str, @"(.{" + pos + "})")
                                 .Where(s => s.Length > 0));
}
string Hyphenate(string str, int pos) {
    return String.Join("-", Regex.Split(str, @"(?<=\G.{" + pos + "})(?!$)"));
}
或者像这样:

string Hyphenate(string str, int pos) {
    return String.Join("-",
        str.Select((c, i) => new { c, i })
           .GroupBy(x => x.i / pos)
           .Select(g => String.Join("", g.Select(x => x.c))));
}
string Hyphenate(string str, int pos) {
    return String.Join("-",
        Enumerable.Range(0, (str.Length - 1) / pos + 1)
            .Select(i => str.Substring(i * pos, Math.Min(str.Length - i * pos, pos))));
}
string Hyphenate(string str, int pos) {
    return String.Join("-", Regex.Split(str, @"(.{" + pos + "})")
                                 .Where(s => s.Length > 0));
}
string Hyphenate(string str, int pos) {
    return String.Join("-", Regex.Split(str, @"(?<=\G.{" + pos + "})(?!$)"));
}
使用StringBuilder:

string input = "12345678"; //could be any length
int position = 3; //could be any other number
StringBuilder sb = new StringBuilder();

for (int i = 0; i < input.Length; i++)
{
    if (i % position == 0){
        sb.Append('-');
    }
    sb.Append(input[i]);
}
string formattedString = sb.ToString();
string input=“12345678”//可以是任意长度
int位置=3//可能是其他号码
StringBuilder sb=新的StringBuilder();
for(int i=0;i
for语句的威力:

string str = "12345667889";
int loc = 3;

for(int ins=loc; ins < str.Length; ins+=loc+1)
   str = str.Insert(ins,"-");

你遇到了什么需要帮助的问题?所以。。。每三个角色?欢迎来到StackOverflow。您需要向我们提供您试图实现的确切细节,如果可能,还需要提供您已经尝试过的代码。你需要什么还不清楚,因为你给了我们3个例子——全部6个字符的长度——告诉我们字符的数量可能不同,但没有告诉我们应该采用什么格式。你可以直接编辑你的问题,你不需要评论。我不明白为什么需要重新打开。请编辑您的文章与您的尝试到目前为止;这是一个先决条件,而不仅仅是给我们一个需求清单。你不认为按职位增加要快得多吗?@Hogan你说得对,这会更快。但是我不知道插入法。我想我学到了一些东西,XDSubstring可以让你不用insert就可以完成。第二个版本是+1,但是你的第一个代码部分使用了
String.insert
,它会返回新的字符串call@Fyodor-没错,第一个是快速完成的,以展示如何使用for语句完成。作为旁注,p.s.w.g的Linq答案创建的字符串与我的第一个代码示例一样多或更多。当然!我也喜欢它的简洁性,很好的例子,只是注意到第二个有点不同,有点困惑。@Fyodor-我本可以在第二个例子中使用与第一个相同的代码,但是通过引用传递与通过值传递的问题会使新的编码人员很难理解代码。。。我觉得这更清楚,因为它看起来像它做的。