473,813 Members | 3,529 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 1729
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
2255
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 new string for every character (since strings are immutable) which seems very inneficient -...
8
4241
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 best way to do this?
15
3635
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
4013
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
4286
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
14064
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
2113
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 as commands and the Text grouping ends up grouping words delimited by spaces. Here is the sample...
4
1815
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
1366
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 INdex)Both start at 0..old index to always point to the current starting postion for the scan and...
13
4530
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
9734
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10667
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10139
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9222
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5568
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4358
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
2
3885
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3030
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.