我如何拆分('')的字符串,而忽略逗号引号之间? [英] How can I Split(',') a string while ignore commas in between quotes?
问题描述
我使用的是 .Split('')对,我知道有逗号分隔值的字符串
的方法,我想分开并投入这些值一个的String []
对象。这对于像这样的字符串的伟大工程:
I am using the .Split(',')
method on a string that I know has values delimited by commas and I want those values to be separated and put into a string[]
object. This works great for strings like this:
78,969.82,GW440,
但价值观开始时第二个值达到1000,像在这个例子中显得与众不同:
But the values start to look different when that second value goes over 1000, like the one found in this example:
79,1,013.42,GW450,...
。
这些值是从我使用内置在控件的电子表格控件来 ExportToCsv(...)
方法和解释了为什么一个格式化版本的实际数值。
These values are coming from a spreadsheet control where I use the controls built in ExportToCsv(...)
method and that explains why a formatted version of the actual numerical value.
有没有一种方法可以让我得到的报价里面的 .Split('')
方法忽略逗号?我真的不想要的值1,013.42
被分裂为1
和 013.42
。
Is there a way I can get the .Split(',')
method to ignore commas inside of quotes? I don't actually want the value "1,013.42"
to be split up as "1
and 013.42"
.
任何想法?谢谢!
我真的想这样做没有纳入第三方工具,我用例真不涉及除了这个其他许多情况下,即使是我的工作的解决方案的一部分,有这样的整合在目前并没有真正受益的人的工具。我希望有些事情很快就解决了,我是缺少此特定用例,但现在是周末,我去看看,如果我不能给多一个更新这个问题上周一的解决办法,我最终会了。谢谢大家对你帮助,到目前为止,我会评估每个进一步回答周一。
I really would like to do this without incorporating a 3rd party tool as my use case really doesn't involve many other cases besides this one and even though it is part of my work's solution, having a tool like that incorporated doesn't really benefit anyone at the moment. I was hoping there was something quick to solve this particular use case that I was missing, but now that it is the weekend, I'll see if I can't give one more update to this question on Monday with the solution I eventually come up with. Thank you everyone for you assistance so far, I'll will assess each answer further on Monday.
推荐答案
这是一个相当直未来,我们这里的几个项目中使用CSV阅读器的实现。易于使用和处理那些你所谈论的情况。
This is a fairly straight forward CSV Reader implementation we use in a few projects here. Easy to use and handles those cases you are talking about.
首先,CSV类
public static class Csv
{
public static string Escape(string s)
{
if (s.Contains(QUOTE))
s = s.Replace(QUOTE, ESCAPED_QUOTE);
if (s.IndexOfAny(CHARACTERS_THAT_MUST_BE_QUOTED) > -1)
s = QUOTE + s + QUOTE;
return s;
}
public static string Unescape(string s)
{
if (s.StartsWith(QUOTE) && s.EndsWith(QUOTE))
{
s = s.Substring(1, s.Length - 2);
if (s.Contains(ESCAPED_QUOTE))
s = s.Replace(ESCAPED_QUOTE, QUOTE);
}
return s;
}
private const string QUOTE = "\"";
private const string ESCAPED_QUOTE = "\"\"";
private static char[] CHARACTERS_THAT_MUST_BE_QUOTED = { ',', '"', '\n' };
}
然后一个相当不错的阅读器实现 - 如果你需要它。你应该能够做你需要的正上方CSV类的东西。
Then a pretty nice Reader implementation - If you need it. You should be able to do what you need with just the CSV class above.
public sealed class CsvReader : System.IDisposable
{
public CsvReader(string fileName)
: this(new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
}
public CsvReader(Stream stream)
{
__reader = new StreamReader(stream);
}
public System.Collections.IEnumerable RowEnumerator
{
get
{
if (null == __reader)
throw new System.ApplicationException("I can't start reading without CSV input.");
__rowno = 0;
string sLine;
string sNextLine;
while (null != (sLine = __reader.ReadLine()))
{
while (rexRunOnLine.IsMatch(sLine) && null != (sNextLine = __reader.ReadLine()))
sLine += "\n" + sNextLine;
__rowno++;
string[] values = rexCsvSplitter.Split(sLine);
for (int i = 0; i < values.Length; i++)
values[i] = Csv.Unescape(values[i]);
yield return values;
}
__reader.Close();
}
}
public long RowIndex { get { return __rowno; } }
public void Dispose()
{
if (null != __reader) __reader.Dispose();
}
//============================================
private long __rowno = 0;
private TextReader __reader;
private static Regex rexCsvSplitter = new Regex(@",(?=(?:[^""]*""[^""]*"")*(?![^""]*""))");
private static Regex rexRunOnLine = new Regex(@"^[^""]*(?:""[^""]*""[^""]*)*""[^""]*$");
}
然后你可以这样使用它。
Then you can use it like this.
var reader = new CsvReader(new FileStream(file, FileMode.Open));
请注意:这将打开现有的CSV文件,但可以相当容易地进行修改,以采取的String []
像你所需要的。
Note: This would open an existing CSV file, but can be modified fairly easily to take a string[]
like you need.
这篇关于我如何拆分('')的字符串,而忽略逗号引号之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!