C# 替换逗号分隔列表中单个字段中的字符

C# 替换逗号分隔列表中单个字段中的字符,c#,string,C#,String,我的c代码中有字符串 我想用“ef”替换“e,f”,也就是说,倒逗号里面的“e,f”应该用空格替换 我尝试使用string.split,但它对我不起作用 使用Split()和Join(): string input=“a、b、c、d、\'e、f\'、g、h”; string[]pieces=input.Split(“”); 对于(int i=1;i

我的c代码中有字符串

我想用“ef”替换“e,f”,也就是说,倒逗号里面的“e,f”应该用空格替换

我尝试使用string.split,但它对我不起作用

使用
Split()
Join()

string input=“a、b、c、d、\'e、f\'、g、h”;
string[]pieces=input.Split(“”);
对于(int i=1;i<件长;i+=2)
{
片段[i]=string.Join(“,片段[i].Split(',');
}
字符串输出=string.Join(“\”,片段);
控制台写入线(输出);
//输出:a、b、c、d、e、f、g、h
使用
Split()
Join()

string input=“a、b、c、d、\'e、f\'、g、h”;
string[]pieces=input.Split(“”);
对于(int i=1;i<件长;i+=2)
{
片段[i]=string.Join(“,片段[i].Split(',');
}
字符串输出=string.Join(“\”,片段);
控制台写入线(输出);
//输出:a、b、c、d、e、f、g、h

好的,我不想费心去想一个正则表达式方法,所以我将提供一个老式的循环方法,它将起作用:

string DoReplace(string input)
{
    bool isInner = false;//flag to detect if we are in the inner string or not
    string result = "";//result to return

    foreach(char c in input)//loop each character in the input string
    {
        if(isInner && c == ',')//if we are in an inner string and it is a comma, append space
            result += " ";
        else//otherwise append the character
            result += c;

        if(c == '"')//if we have hit an inner quote, toggle the flag
            isInner = !isInner;
    }

    return result;
}
string source = "a,b,c,d,\"e,f\",g,h";
string pattern = "\"([\\w]),([\\w])\"";
string replace = "\"$1 $2\"";
string result = Regex.Replace(source, pattern, replace);
Console.WriteLine(result); // a,b,c,d,"e f",g,h

注意:这个解决方案假设只有一个级别的内部引号,例如你不能有
“a,b,c,”d,e,“f,g”,h“,i,j”
——因为这简直是疯狂

好的,我不想费心去想正则表达式方法,所以我将提供一种老式的循环方法,它将起作用:

string DoReplace(string input)
{
    bool isInner = false;//flag to detect if we are in the inner string or not
    string result = "";//result to return

    foreach(char c in input)//loop each character in the input string
    {
        if(isInner && c == ',')//if we are in an inner string and it is a comma, append space
            result += " ";
        else//otherwise append the character
            result += c;

        if(c == '"')//if we have hit an inner quote, toggle the flag
            isInner = !isInner;
    }

    return result;
}
string source = "a,b,c,d,\"e,f\",g,h";
string pattern = "\"([\\w]),([\\w])\"";
string replace = "\"$1 $2\"";
string result = Regex.Replace(source, pattern, replace);
Console.WriteLine(result); // a,b,c,d,"e f",g,h

注意:这个解决方案假设只有一个级别的内部引号,例如你不能有
“a,b,c,”d,e,“f,g”,h“,i,j”
——因为这简直是疯狂

对于只需匹配一对字母的场景,以下正则表达式将起作用:

string DoReplace(string input)
{
    bool isInner = false;//flag to detect if we are in the inner string or not
    string result = "";//result to return

    foreach(char c in input)//loop each character in the input string
    {
        if(isInner && c == ',')//if we are in an inner string and it is a comma, append space
            result += " ";
        else//otherwise append the character
            result += c;

        if(c == '"')//if we have hit an inner quote, toggle the flag
            isInner = !isInner;
    }

    return result;
}
string source = "a,b,c,d,\"e,f\",g,h";
string pattern = "\"([\\w]),([\\w])\"";
string replace = "\"$1 $2\"";
string result = Regex.Replace(source, pattern, replace);
Console.WriteLine(result); // a,b,c,d,"e f",g,h
分解模式,它将匹配任何存在“X,X”序列的实例,其中X是任意字母,并将其替换为完全相同的序列,在字母之间使用空格而不是逗号

如果需要匹配多个字母等,您可以很容易地扩展它

对于需要替换的引号内可以有多个用逗号分隔的字母的情况,下面可以为您提供帮助。示例文本是
a,b,c,d,“e,f,a”,g,h

string source = "a,b,c,d,\"e,f,a\",g,h";
string pattern = "\"([ ,\\w]+),([ ,\\w]+)\"";
string replace = "\"$1 $2\"";
string result = source;
while (Regex.IsMatch(result, pattern)) {
    result = Regex.Replace(result, pattern, replace);
}
Console.WriteLine(result); // a,b,c,d,"e f a",g,h

此操作与第一个操作类似,但只需删除由引号包围的字母夹在中间的任何逗号,并重复此操作,直到删除所有大小写。

对于只需匹配一对字母的场景,以下正则表达式将起作用:

string DoReplace(string input)
{
    bool isInner = false;//flag to detect if we are in the inner string or not
    string result = "";//result to return

    foreach(char c in input)//loop each character in the input string
    {
        if(isInner && c == ',')//if we are in an inner string and it is a comma, append space
            result += " ";
        else//otherwise append the character
            result += c;

        if(c == '"')//if we have hit an inner quote, toggle the flag
            isInner = !isInner;
    }

    return result;
}
string source = "a,b,c,d,\"e,f\",g,h";
string pattern = "\"([\\w]),([\\w])\"";
string replace = "\"$1 $2\"";
string result = Regex.Replace(source, pattern, replace);
Console.WriteLine(result); // a,b,c,d,"e f",g,h
分解模式,它将匹配任何存在“X,X”序列的实例,其中X是任意字母,并将其替换为完全相同的序列,在字母之间使用空格而不是逗号

如果需要匹配多个字母等,您可以很容易地扩展它

对于需要替换的引号内可以有多个用逗号分隔的字母的情况,下面可以为您提供帮助。示例文本是
a,b,c,d,“e,f,a”,g,h

string source = "a,b,c,d,\"e,f,a\",g,h";
string pattern = "\"([ ,\\w]+),([ ,\\w]+)\"";
string replace = "\"$1 $2\"";
string result = source;
while (Regex.IsMatch(result, pattern)) {
    result = Regex.Replace(result, pattern, replace);
}
Console.WriteLine(result); // a,b,c,d,"e f a",g,h

此操作与第一个类似,但只是删除任何由引号包围的字母夹住的逗号,并重复此操作,直到删除所有大小写。

以下是一个有点脆弱但简单的解决方案:

string.Join("\"", line.Split('"').Select((s, i) => i % 2 == 0 ? s : s.Replace(",", " ")))

它是脆弱的,因为它不处理在双引号中转义双引号的CSV样式。

这里有一个稍微脆弱但简单的解决方案:

string.Join("\"", line.Split('"').Select((s, i) => i % 2 == 0 ? s : s.Replace(",", " ")))

它很脆弱,因为它不能处理在双引号中转义双引号的CSV格式。

使用以下代码:

        string str = "a,b,c,d,\"e,f\",g,h";
        string[] str2 = str.Split('\"');
        var str3 = str2.Select(p => ((p.StartsWith(",") || p.EndsWith(",")) ? p : p.Replace(',', ' '))).ToList();
        str = string.Join("", str3);

使用以下代码:

        string str = "a,b,c,d,\"e,f\",g,h";
        string[] str2 = str.Split('\"');
        var str3 = str2.Select(p => ((p.StartsWith(",") || p.EndsWith(",")) ? p : p.Replace(',', ' '))).ToList();
        str = string.Join("", str3);


我只是想说清楚。。。整件事都是一串,对吗?比如,它是一个字符串中的一个字符串?@musefan是的,整个东西都是字符串。一个子字符串中可能有多个逗号吗?例如,`“e,f,g`,两者都应该用空格替换?@musefan是的,这是可能的&两个逗号都应该用空格替换。请编辑您的答案,以包含这些注释中讨论的确切要求必须清楚。。。整件事都是一串,对吗?比如,它是一个字符串中的一个字符串?@musefan是的,整个东西都是字符串。一个子字符串中可能有多个逗号吗?例如,`“e,f,g`,两者都应该用空格替换?@musefan是的,这是可能的&两个逗号都应该用空格替换。请编辑您的答案,以包括这些注释中讨论的确切要求,这些注释将替换所有逗号。OP明确表示他们只想替换引号中的字符串。@Marcelocontos我会调整我的答案。@StevenLiekens我的字符串是a,b,c,d,“e,f”,g,h&而不是a,b,c,d,e,f,g,h这个。@vaibhavshah我已经更新了我的解决方案,很抱歉误解。你可以通过用
I+=2
替换
I++
来避免测试。此外,您忘了重新引入引号。这将替换所有逗号。OP明确表示他们只想替换引号中的字符串。@Marcelocontos我会调整我的答案。@StevenLiekens我的字符串是a,b,c,d,“e,f”,g,h&而不是a,b,c,d,e,f,g,h这个。@vaibhavshah我已经更新了我的解决方案,很抱歉误解。你可以通过用
I+=2
替换
I++
来避免测试。此外,你忘了重新引入引号。如果你阅读了关于这个问题的评论,OP说它还需要处理内部引号中的两个以上的字母。例如,
“d,e,f”
我是否遗漏了什么?。。。OP明确表示需要在内部字符串中处理多个逗号,但接受了这个答案?@musefan I更新了答案,以解决第二种情况下的多个字母。如果您阅读了对该问题的评论,OP表示还需要处理内部引号中的两个以上字母。例如,
“d,e,f”
我是否遗漏了什么?。。。OP清楚地说明了需要处理的不仅仅是o