当数据可以包含逗号时在逗号上分割字符串 [英] Splitting string on commas when data can contain commas

查看:52
本文介绍了当数据可以包含逗号时在逗号上分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV文件(我没有设计过,现在无法更改,也永远无法更改),该文件包含以下内容:

姓氏,姓氏",是,否,字符串,等等

正如您在此处看到的那样,第一个 不是要分割字符串的逗号.请注意,该逗号用引号引起来.

因此,一个简单的 string.split(',')显然不起作用,因为它将为我提供上述字符串的长度为7而不是6的数组.

有没有办法解决这个问题?我当时在考虑使用正则表达式来分割字符串,但是我对正则表达式的能力不足以想到仅在未用引号引起来的逗号上进行分割的模式.

我可以想到通过逐个字符读取每个字符串来实现此目的的丑陋,笨拙的方法,但这将是万不得已的方法,因为我敢肯定还有更好的方法!

解决方案

我知道这里有很多人认为不应该使用逐字符比较,并且会强烈反对我,但是我不相信公司并非只有Microsoft这样的人才应该进行这种编程.

毕竟,Split会逐个字符地进行比较,所以当您调用不能完全满足您期望的现有代码时,为什么它变得那么丑陋呢?

无论如何,我的方法是编写自己的代码.而且我已经在 http:上在线发布了代码://www.blackbeltcoder.com/Articles/files/reading-and-writing-csv-files-in-c .

I have a CSV file (which I didn't design and I can't change now nor will I ever be able to change it) that contains lines like the following:

"Surname, Firstname", yes, no, somestring, whatever, etc

As you can see here, the first , is not a comma on which I'd want to split the string. Notice that this particular comma is enclosed within the quotation marks.

Because of this, a simple string.split(',') obviously won't work, as it would give me an array of length 7 for the above string instead of 6.

Is there a way to get around this? I was thinking of using regex to split the string instead but I'm not competent enough in regex to think of a pattern that would only split on commas that are not enclosed inside quotation marks.

I can think of ugly, hacky ways to do it by reading each string char by char but this would have to be a last resort as I'm sure there's a better way to do it!

解决方案

I know there's a lot of people here who think character-by-character comparisons should never be used and will strongly disagree with me but I'm not convinced companies like Microsoft aren't the only ones who should be doing that sort of programming.

Afterall, Split does character-by-character comparisons so why is it any less ugly when you call existing code that doesn't quite do exactly what you want?

At any rate, my approach was to write my own code. And I've posted the code online at http://www.blackbeltcoder.com/Articles/files/reading-and-writing-csv-files-in-c.

这篇关于当数据可以包含逗号时在逗号上分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆