473,554 Members | 2,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

parsing words in a string

Hi,

Is there a class that can handle splitting of a string on a comma such
that the commas in quotes are ignored?

I know we can use Text::ParseWord s directive in perl to do this, but I
am new to C#.Net and couldn't find anything similar.

For example

string str = "one field, two field, \"field val one, field val two,
field val three\", three field" ;

Then I should get the following in a str_arr

str_arr[0] = "one field"
str_arr[1] = "two filed"
str_arr[2] = "field val one, field val two, field val three"
str_arr[3] = "three field"

Thanks in advance,

Ashoo.

Jan 6 '06 #1
19 1710
string[] str_arr = str.Split(",");

Jan 6 '06 #2
string [] str_arr = str.Split(',');

does not work. I had tried it. It gives me

str_arr[0] = "one field"
str_arr[1] = "two filed"
str_arr[2] = "\"field val one"
str_arr[3] = "field val two"
str_arr[4] = "field val three\""
str_arr[5] = "three field"

Instead of
str_arr[0] = "one field"
str_arr[1] = "two filed"
str_arr[2] = "field val one, field val two, field val three"
str_arr[3] = "three field"

look at the str_arr[2] value.

Thanks,
Ashoo
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Jan 6 '06 #3
hi,
you can use Regular Expression which can give u such results. here is
one regular expression which will fetch you your desired result. i
assumed that you are using VS2003.

(?:(?<Vals>.*?) ,)+"(?<Vals>.*? )"(?:,(?<Vals>. *?))+$

the ouput of your given example will be like this :

SubMatch: [Vals]
1:one field
2:two field
3:field val one, field val two,field val three
4:three field

try it out

Jan 7 '06 #4
At this time using split more than once seems to be the only option. I
cant recollect any other function that can help you do this quicker.
how do you use that function in perl? put some code and may be that can
ring the bell for some csharpers.

thanks

Jan 7 '06 #5
hi,
you can use Regular Expression which can give u such results. here is
one regular expression which will fetch you your desired result. i
assumed that you are using VS2003.

(?:(?<Vals>.*?) ,)+"(?<Vals>.*? )"(?:,(?<Vals>. *?))+$

the ouput of your given example will be like this :

SubMatch: [Vals]
1:one field
2:two field
3:field val one, field val two,field val three
4:three field

try it out

Jan 7 '06 #6
Thanks, I will try it out.

Ashoo

--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Jan 7 '06 #7
This is how you cld do it in perl

#!/usr/bin/perl

use Text::ParseWord s;

$line = "one field, two field, \"field val one, field val two,field val
three\", three field" ;

my @line = &parse_line('\, ', 0, $line);

for (int i=0; i<$#line; i++)
print $line[i];

Thanks,
Ashoo
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Jan 7 '06 #8
public System.Collecti ons.ArrayList parseWords(stri ng s)
{

if (s == null)
{
return (null);
}
bool bQuote = false;
System.Collecti ons.ArrayList al = new ArrayList();
System.Text.Str ingBuilder sTemp = new StringBuilder() ;

for (int i = 0; i < s.Length; i++)
{
switch (s[i])
{
case ',':
if (bQuote == false)
{
al.Add(sTemp.To String());
sTemp.Length = 0;
}
else
{
sTemp.Append(s[i]);
}
break;
case '\"':
if (bQuote == true)
{
bQuote = false;
}
else
{
bQuote = true;
}

//requirement:: remove quote character
//sTemp.Append(s[i]);
break;
default:
sTemp.Append(s[i]);
break;
}
}

if (sTemp.Length > 0)
{
al.Add(sTemp.To String());
sTemp.Length = 0;
}

return (al);
}

"asrs63" wrote:
Hi,

Is there a class that can handle splitting of a string on a comma such
that the commas in quotes are ignored?

I know we can use Text::ParseWord s directive in perl to do this, but I
am new to C#.Net and couldn't find anything similar.

For example

string str = "one field, two field, \"field val one, field val two,
field val three\", three field" ;

Then I should get the following in a str_arr

str_arr[0] = "one field"
str_arr[1] = "two filed"
str_arr[2] = "field val one, field val two, field val three"
str_arr[3] = "three field"

Thanks in advance,

Ashoo.

Jan 7 '06 #9
Have you tried using regular expressions, instead of the Split()?

~~~~~~~~~~
"Ashoo Sharda" <as**********@r eyrey.com> wrote in message
news:%2******** *********@TK2MS FTNGP15.phx.gbl ...
string [] str_arr = str.Split(',');

does not work. I had tried it. It gives me

str_arr[0] = "one field"
str_arr[1] = "two filed"
str_arr[2] = "\"field val one"
str_arr[3] = "field val two"
str_arr[4] = "field val three\""
str_arr[5] = "three field"

Instead of
str_arr[0] = "one field"
str_arr[1] = "two filed"
str_arr[2] = "field val one, field val two, field val three"
str_arr[3] = "three field"

look at the str_arr[2] value.

Thanks,
Ashoo
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Jan 7 '06 #10

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

Similar topics

3
2232
by: Fuzzyman | last post by:
I want to parse some text and generate an output that is similar but not identical to the input. The string I produce will be of similar length to the input string - but a bit longer. I'm parsing character by character and adding the characters of the input string to the output until I come to ones I want to modify. This means creating a...
8
4202
by: Anders Eriksson | last post by:
Hello! I want to extract some info from a some specific HTML pages, Microsofts International Word list (e.g. http://msdn.microsoft.com/library/en-us/dnwue/html/swe_word_list.htm). I want to take all the words, both English and the other language and create a dictionary. so that I can look up About and get Om as the answer. How is the...
15
3605
by: Freddie | last post by:
Happy new year! Since I have run out of alcohol, I'll ask a question that I haven't really worked out an answer for yet. Is there an elegant way to turn something like: > moo cow "farmer john" -zug into: ,
19
3989
by: ARK | last post by:
I am writing a search program in ASP(VBScript). The user can enter keywords and press submit. The user can separate the keywords by spaces and/or commas and key words may contain plain words, single quoted strings (phrases), double quoted strings (phrases). For example: Keywords: Jack, Jill, Jim, "Timothy Brown", Mary OR
4
4274
by: meldrape | last post by:
Hello, I need to parse a long string into no more than 30 character chunks, but I also need to leave the words intact. Right now, I am using: For intStart = 1 to Len(strOriginal) by 30 strPrint = Mid$(strOriginal, intStart, 30) Print #detailFile, Tab(1), intStart, Tab(4), strPrint Next intStart
5
14043
by: Aleksandar Matijaca | last post by:
Hi there, I am in some need of help. I am trying to parse using the apache sax parser a file that has vaid UTF-8 characters - I keep end up getting a sun.io.MalformedInputException error. This is my code:
2
2083
by: JaythePCguy | last post by:
Hi, I am trying to write a text parser to group all nonprintable and control characters, spaces and space delimited words in different groups using Regex class. Using a parsing of (?<Commands>)|(?<Spaces>)|(?<Text>+) on my sample text of \tOne\ncar red \fcar\a blue car\r\n \r\n does not work as indetended. Specially, the spaces are grouped...
4
1806
by: william | last post by:
Hello, I've imported an excel spreadsheet with a Name column which is formatted as Last, First, MI. Some examples I have in the Name column: Smith, Ellen P. Jones, Mary Jane Blackman-Pearson, Betsy D. Wright, George
1
1354
by: kellysgirl | last post by:
Im not good at parsing strings....and Ive been driving myslef nuts This is what I need to do....use an if/else statement to validate thata delimeter has been selected. These delimeters being comma, space and cr-lf. Parse text box contents----- Parse string to break out the words involving a loop and 2 pointer variable(old INdex and New...
13
4475
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple command set consisting of several single letter commands which take no arguments. A few additional single letter commands take arguments:
0
7539
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7819
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7897
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5165
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3589
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3566
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2030
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1149
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
858
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.