1

我正在尝试读取以逗号分隔的文本文件中的数据。我的问题是我的一个数据片段中有一个逗号。文本文件的示例如下:

a, b, "c, d", e, f.

我希望能够在 and 之间使用逗号c并将d其更改为分号,以便我仍然可以使用该string.Split()方法。

 using (StreamReader reader = new StreamReader("file.txt"))
 {
    string line;
    while ((line = reader.ReadLine ()) != null) {
    bool firstQuote = false;
    for (int i = 0; i < line.Length; i++) 
    {
      if (line [i] == '"' ) 
      {
         firstQuote = true;
      }  
      else if (firstQuote == true) 
      {
         if (line [i] == '"')
         {
            break;
         } 
         if ((line [i] == ',')) 
         {
            line = line.Substring (0, i) + ";" + line.Substring (i + 1, (line.Length - 1) - i);
         }
      }
    }
    Console.WriteLine (line);
 }

我有问题。而不是生产

a, b, "c; d", e, f

它正在生产

a, b, "c; d"; e; f

它将以下所有逗号替换为分号,而不仅仅是引号中的逗号。有人可以帮我修复我现有的代码吗?

4

4 回答 4

2

基本上,如果你找到一个结束语",你就会认出它是一个开场报价。

换行:

firstQuote = true;

firstQuote = !firstQuote;

它应该可以工作。

于 2012-12-05T11:06:02.017 回答
0
    string line = "a,b,mc,dm,e,f,mk,lm,g,h";

    string result =replacestr(line, 'm', ',', ';');


    public string replacestr(string line,char seperator,char oldchr,char newchr)
    {
        int cnt = 0;
        StringBuilder b = new StringBuilder();
        foreach (char chr in line)
        {
            if (cnt == 1 && chr == seperator)
            {
                b[b.ToString().LastIndexOf(oldchr)] = newchr;
                b.Append(chr);
                cnt = 0;
            }
            else
            {
                if (chr == seperator)
                    cnt = 1;
                b.Append(chr);
            }
        }
        return b.ToString();
    }
于 2012-12-05T12:09:01.683 回答
0

击中第二个引号后,您需要将 firstquote 重置为 false。

                    else if (firstQuote == true) {
                        if (line [i] == '"') {
                            firstquote = false;
                            break;
                        }
于 2012-12-05T11:12:07.863 回答
0

这是一个获得所需结果的简单应用程序

    static void Main(string[] args)
    {
        String str = "a,b,\"c,d\",e,f,\"g,h\",i,j,k,l,\"m,n,o\"";
        int firstQuoteIndex = 0;
        int secodQuoteIndex = 0;
        Console.WriteLine(str);
        bool iteration = false;

        //String manipulation
        //if count is even then count/2 is the number of pairs of double quotes we are having
        //so we have to traverse count/2 times.
        int count = str.Count(s => s.Equals('"'));
        if (count >= 2)
        {
            firstQuoteIndex = str.IndexOf("\"");
            for (int i = 0; i < count / 2; i++)
            {
                if (iteration)
                {
                    firstQuoteIndex = str.IndexOf("\"", firstQuoteIndex + 1);
                }
                secodQuoteIndex = str.IndexOf("\"", firstQuoteIndex + 1);
                string temp = str.Substring(firstQuoteIndex + 1, secodQuoteIndex - (firstQuoteIndex + 1));                    
                firstQuoteIndex = secodQuoteIndex + 1;
                if (count / 2 > 1)
                    iteration = true;                    
                string temp2= temp.Replace(',', ';');
                str = str.Replace(temp, temp2);
                Console.WriteLine(temp);
            }
        }
        Console.WriteLine(str);
        Console.ReadLine();
    }

如有疑问,请随时询问

于 2012-12-05T12:01:27.780 回答