473,385 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

String Question

Happy New Year to all! :D

I am currently developoing an application that imports data from a CSV file.
Each comma represents an array item that I need to extract data with.

My problem is this...

I am encountering a string that has the example below:

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

----The data that has double quotes is considered to be one column.

Whenever I am using the String.Split(',') method, it keeps on breaking the
commas inside the quotes.

How can I prevent this so that it would treat the commas within the quotes
as a single unit?

I would really appreciate the help!

More Power to all! :D

Ann

Nov 15 '05 #1
5 3208
Rather than parsing the data manually, you might want to use the ODBC driver
for text files. It handles all these little details for you, and allows you
to see the file as if it were a database table (you can execute sql
statements, etc).

And happy new year to you too!

-Rob Teixeira [MVP]

"Ann Marinas" <an*********@xtra.co.nz> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Happy New Year to all! :D

I am currently developoing an application that imports data from a CSV file. Each comma represents an array item that I need to extract data with.

My problem is this...

I am encountering a string that has the example below:

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

----The data that has double quotes is considered to be one column.

Whenever I am using the String.Split(',') method, it keeps on breaking the
commas inside the quotes.

How can I prevent this so that it would treat the commas within the quotes
as a single unit?

I would really appreciate the help!

More Power to all! :D

Ann

Nov 15 '05 #2
If only String.Split used strings instead of chars/char
arrays to specify the delimiter/s!

One solution that springs to mind, while not pretty, is
to just iterate through your array looking for elements
that contain double quotes. Assuming your original
string was correctly formatted such that all opening
double quotes had corresponding closing quotes, it should
work (although might take a while if your CSV strings are
huge). Here's a sample of what I mean (note that I have
not parsed, compiled or tested this code fragment - typos
or syntax errors are free bonuses! Also you could
probably make this more elegant - this is just to
illustrate the concept):

string origStr = "a, b, c, \"d,e,f,g\", abcdef";
string[] newArray = origStr.Split(',');
ArrayList finalValues = new ArrayList();
bool foundFirstQuote = false, foundSecondQuote = false;
StringBuilder newElem;
for (i=0; i<newArray.Length; i++)
{
string elem = newArray[i];
if ((elem.IndexOf("\"") >= 0) || foundFirstQuote)
{
// Our element looks like "a or a"
if (foundFirstQuote)
{
// We got the last value in the multival column
foundSecondQuote = true;
}
else
{
// We got the first val in the multival column
// - start building a single string of comma
// separated values until we hit the next
// double quote
newElem = new StringBuilder();
foundFirstQuote = true;
}
if (newElem.Length > 0)
{
// Separate the values with commas
newElem.Append(",");
}
// Add the new value to the comma-separated list
newElem.Append(elem);
if (foundSecondQuote)
{
// We have now processed the last value in
// the list - add the whole list to our
// ArrayList.
finalValues.Add(newElem.ToString());
foundFirstQuote = false;
foundSecondQuote = false;
}
}
else
{
// This value contained no double quotes - just
// add it as-is to the ArrayList.
finalValues.Add(elem);
}
}

Hope this helps. Sorry there was not a simpler answer!
-----Original Message-----

Happy New Year to all! :D

I am currently developoing an application that imports
data from a CSV file. Each comma represents an array item
that I need to extract data with.

My problem is this...

I am encountering a string that has the example below:

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

----The data that has double quotes is considered to be
one column.

Whenever I am using the String.Split(',') method, it
keeps on breaking the commas inside the quotes.

How can I prevent this so that it would treat the commas
within the quotes as a single unit?

I would really appreciate the help!
Nov 15 '05 #3
Greetings
If only String.Split used strings instead of chars/char
arrays to specify the delimiter/s!
I hit that split issue recently. I came up with the following hack solution:

string s = "need<br>to<br>split<br>this<br>string<br>into<br> an<br>array<br>";
string[] parts = s.Replace("<br>", "~").Split('~');

What about Regex.Split()?

"Andrew Warren" <an*******@discussions.microsoft.com> wrote in message news:<05****************************@phx.gbl>... If only String.Split used strings instead of chars/char
arrays to specify the delimiter/s!

One solution that springs to mind, while not pretty, is
to just iterate through your array looking for elements
that contain double quotes. Assuming your original
string was correctly formatted such that all opening
double quotes had corresponding closing quotes, it should
work (although might take a while if your CSV strings are
huge). Here's a sample of what I mean (note that I have
not parsed, compiled or tested this code fragment - typos
or syntax errors are free bonuses! Also you could
probably make this more elegant - this is just to
illustrate the concept):

string origStr = "a, b, c, \"d,e,f,g\", abcdef";
string[] newArray = origStr.Split(',');
ArrayList finalValues = new ArrayList();
bool foundFirstQuote = false, foundSecondQuote = false;
StringBuilder newElem;
for (i=0; i<newArray.Length; i++)
{
string elem = newArray[i];
if ((elem.IndexOf("\"") >= 0) || foundFirstQuote)
{
// Our element looks like "a or a"
if (foundFirstQuote)
{
// We got the last value in the multival column
foundSecondQuote = true;
}
else
{
// We got the first val in the multival column
// - start building a single string of comma
// separated values until we hit the next
// double quote
newElem = new StringBuilder();
foundFirstQuote = true;
}
if (newElem.Length > 0)
{
// Separate the values with commas
newElem.Append(",");
}
// Add the new value to the comma-separated list
newElem.Append(elem);
if (foundSecondQuote)
{
// We have now processed the last value in
// the list - add the whole list to our
// ArrayList.
finalValues.Add(newElem.ToString());
foundFirstQuote = false;
foundSecondQuote = false;
}
}
else
{
// This value contained no double quotes - just
// add it as-is to the ArrayList.
finalValues.Add(elem);
}
}

Hope this helps. Sorry there was not a simpler answer!
-----Original Message-----

Happy New Year to all! :D

I am currently developoing an application that imports
data from a CSV file. Each comma represents an array item
that I need to extract data with.

My problem is this...

I am encountering a string that has the example below:

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

----The data that has double quotes is considered to be
one column.

Whenever I am using the String.Split(',') method, it
keeps on breaking the commas inside the quotes.

How can I prevent this so that it would treat the commas
within the quotes as a single unit?

I would really appreciate the help!

Nov 15 '05 #4
Hi,
[inline]

"Ann Marinas" <an*********@xtra.co.nz> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Happy New Year to all! :D

I am currently developoing an application that imports data from a CSV file. Each comma represents an array item that I need to extract data with.

My problem is this...

I am encountering a string that has the example below:

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

----The data that has double quotes is considered to be one column.

Whenever I am using the String.Split(',') method, it keeps on breaking the
commas inside the quotes.

If you want, you could write your own split function like this, it isn't
that complicated:

public static string[] SmartSplit(string line)
{
ArrayList parts = new ArrayList();
int i, iStart=0;
bool bQuoted=false;

for (i=0; i<line.Length; ++i)
{
switch (line[i])
{
case ',':
if (!bQuoted)
{
parts.Add( line.Substring( iStart, i - iStart ) );
iStart = i + 1;
}
break;
case '"': // read: single double single quote
bQuoted = !bQuoted;
break;
} // end switch
} // end for
parts.Add( line.Substring( iStart, i - iStart ) );

return (string[])parts.ToArray(typeof(string));
}
HTH,
greetings

How can I prevent this so that it would treat the commas within the quotes
as a single unit?

I would really appreciate the help!

More Power to all! :D

Ann

Nov 15 '05 #5
Thank you so much for all of your help!

I really do appreciate it! :)

God Bless!

--Ann

"Ann Marinas" <an*********@xtra.co.nz> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Happy New Year to all! :D

I am currently developoing an application that imports data from a CSV file. Each comma represents an array item that I need to extract data with.

My problem is this...

I am encountering a string that has the example below:

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

----The data that has double quotes is considered to be one column.

Whenever I am using the String.Split(',') method, it keeps on breaking the
commas inside the quotes.

How can I prevent this so that it would treat the commas within the quotes
as a single unit?

I would really appreciate the help!

More Power to all! :D

Ann

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sparks | last post by:
I know this is a stupid question but I can't find the answer. Please tell me where to find reference to this so I can print it out and staple it to my head.. dim I as integer dim missedstr as...
5
by: John Baro | last post by:
I have a richtextbox which I want the "literal" rtf of. richtextbox.rtf returns {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0 when i put this into a string I get...
5
by: Dave | last post by:
I'm receiving info from a com port into a string. I gradually process the string which constantly shortens it. The question is how long can a string be before I need to write some info to disk...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
39
by: sucaba.r | last post by:
I don't know if this is a unique problem, or I'm going about it the wrong way. I currently connect to one of our SQL servers via a priviliged account (by using RUNAS). Works with no problem. I...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.