Code
----------------------
string Line = "\"A\",\"B\",\" C\",\"D\"";
string Line2 = Line.Replace("\ ",\"","\"\",\"\ "");
string [] CSVColumns = Line2.Split("\" ,\"".ToCharArra y());
I expect 4 values in my CSVColumns Array ("A", "B", "C", "D"). But it
returns me 18 values with lot of empty strings.
Let me know if I am missing anything.
Thanks 6 6367
On 9 Jun 2004 18:44:35 -0700, Senthil wrote: Code ---------------------- string Line = "\"A\",\"B\",\" C\",\"D\"";
string Line2 = Line.Replace("\ ",\"","\"\",\"\ "");
string [] CSVColumns = Line2.Split("\" ,\"".ToCharArra y());
The problem is here. You are asking it to split on a set of individual
characters, not on a string. You can't split on a *string* with
String.Split.
Instead, you'll have to use the following:
string [] CSVColumns =
System.Text.Reg ularExpressions .Regex.Split(Li ne2, "\",\"");
I expect 4 values in my CSVColumns Array ("A", "B", "C", "D"). But it returns me 18 values with lot of empty strings.
In any case, the following achieves the same effect in a more efficient
manner.
string line = "\"A\",\"B\",\" C\",\"D\"";
string[] csvColumns = line.Split(',') ;
"Senthil" wrote: Code ---------------------- string Line = "\"A\",\"B\",\" C\",\"D\"";
string Line2 = Line.Replace("\ ",\"","\"\",\"\ "");
Why is this line here? I don't understand the purpose of adding more quotes to your string.
string [] CSVColumns = Line2.Split("\" ,\"".ToCharArra y()); I expect 4 values in my CSVColumns Array ("A", "B", "C", "D"). But it returns me 18 values with lot of empty strings.
string.Split splits the string on *each* character given. You seem to be expecting "," to be treated as a three-character delimiter, but it's actually three one-character delimiters. The empty strings you're getting are the zero-length strings between the quotes and commas in your input string. http://weblogs.asp.net/justin_rogers.../14/89545.aspx
An algorithm for splitting based on string based delimiter.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Jerry" <Je***@discussi ons.microsoft.c om> wrote in message
news:09******** *************** ***********@mic rosoft.com...
"Senthil" wrote:
Code ---------------------- string Line = "\"A\",\"B\",\" C\",\"D\"";
string Line2 = Line.Replace("\ ",\"","\"\",\"\ ""); Why is this line here? I don't understand the purpose of adding more quotes to
your string.
string [] CSVColumns = Line2.Split("\" ,\"".ToCharArra y()); I expect 4 values in my CSVColumns Array ("A", "B", "C", "D"). But it returns me 18 values with lot of empty strings.
string.Split splits the string on *each* character given. You seem to be
expecting "," to be treated as a three-character delimiter, but it's actually
three one-character delimiters. The empty strings you're getting are the
zero-length strings between the quotes and commas in your input string.
Senthil,
In addition to the other comments.
There are three Split functions in .NET:
Use Microsoft.Visua lBasic.Strings. Split if you need to split a string based
on a specific word (string). It is the Split function from VB6.
Use System.String.S plit if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.
Use System.Text.Reg ularExpressions .RegEx.Split to split based
on matching patterns.
By referencing the Microsoft.Visua lBasic assembly in C# you can use the
Strings.Split function to split a string based on a word.
Something like:
string [] CSVColumns = Strings.Split(L ine2, "\",\"", -1,
CompareMethod.B inary);
Hope this helps
Jay
"Senthil" <se***********@ yahoo.com> wrote in message
news:46******** *************** **@posting.goog le.com... Code ---------------------- string Line = "\"A\",\"B\",\" C\",\"D\"";
string Line2 = Line.Replace("\ ",\"","\"\",\"\ "");
string [] CSVColumns = Line2.Split("\" ,\"".ToCharArra y());
I expect 4 values in my CSVColumns Array ("A", "B", "C", "D"). But it returns me 18 values with lot of empty strings. Let me know if I am missing anything. Thanks
"Jerry" <Je***@discussi ons.microsoft.c om> wrote in message news:<09******* *************** ************@mi crosoft.com>... "Senthil" wrote:
Code ---------------------- string Line = "\"A\",\"B\",\" C\",\"D\"";
string Line2 = Line.Replace("\ ",\"","\"\",\"\ "");
Why is this line here? I don't understand the purpose of adding more quotes to your string. string [] CSVColumns = Line2.Split("\" ,\"".ToCharArra y()); I expect 4 values in my CSVColumns Array ("A", "B", "C", "D"). But it returns me 18 values with lot of empty strings.
string.Split splits the string on *each* character given. You seem to be expecting "," to be treated as a three-character delimiter, but it's actually three one-character delimiters. The empty strings you're getting are the zero-length strings between the quotes and commas in your input string.
I am trying to break a csv file. I can't break it by comma as one of
column value itself can have a comma. So, I am replacing all "," with
"","" and break it by ",". That is the reason I have to do that
replace. Now I understood what I missed. Thanks for your help. I will
use the method suggested in the previous message.
Thanks
Senthil
int offset = 0;
int[] offsets = new int[input.Length];
int quotes = 0;
for(int i = 0; i < input.Length; i++) {
if ( input[i] == '\"' ) { quotes++; }
else if ( input[i] == ',' && quotes % 2 == 0 ) {
offsets[offset++] = i;
}
}
The breaker above should use matching quotes to decide wether or not the comma
can be a splitting character. The values left in offsets can then be used to
grab out
particular columns as needed or can be used to provide a full string[] split.
--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers
"Senthil" <se***********@ yahoo.com> wrote in message
news:46******** *************** **@posting.goog le.com... "Jerry" <Je***@discussi ons.microsoft.c om> wrote in message
news:<09******* *************** ************@mi crosoft.com>... "Senthil" wrote:
Code ---------------------- string Line = "\"A\",\"B\",\" C\",\"D\"";
string Line2 = Line.Replace("\ ",\"","\"\",\"\ "");
Why is this line here? I don't understand the purpose of adding more quotes
to your string.
string [] CSVColumns = Line2.Split("\" ,\"".ToCharArra y()); I expect 4 values in my CSVColumns Array ("A", "B", "C", "D"). But it returns me 18 values with lot of empty strings.
string.Split splits the string on *each* character given. You seem to be
expecting "," to be treated as a three-character delimiter, but it's actually
three one-character delimiters. The empty strings you're getting are the
zero-length strings between the quotes and commas in your input string. I am trying to break a csv file. I can't break it by comma as one of column value itself can have a comma. So, I am replacing all "," with "","" and break it by ",". That is the reason I have to do that replace. Now I understood what I missed. Thanks for your help. I will use the method suggested in the previous message.
Thanks
Senthil This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: MetalOne |
last post by:
string.split("") ==>
string.split("",",") ==>
I did not expect these to have different outputs.
I have a string with comma delimited numbers.
There can be zero or more numbers in the string
s = "0x41, 0x42"
I wanted to do
|
by: flam |
last post by:
Hello,
I am having a hard time spliting a string into an array for use in a
search. Here is the situation. The user will input a search string.
Normally I can just split the string by "split /\s+/, $string".
However, I want to allow the user to put words so they appear and are
searched together ie. "search this" would be searched as one...
|
by: Ann Marinas |
last post by:
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
|
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 string. I had it working
somewhat, but ran into problems if one of the highlighted words could also be
part of an HTML tag (such as 'Table' or...
|
by: J Stoodley |
last post by:
I am in a learning curve right now, and want to become well aquanted
with VB.NET. So, I have two questions. 1 is technial the other is
resource related.
1. I need to strip a single character from a string. I will explain
why. I am creating a page that will add users to Active Directory. I
collect the FirstName value (based on a pre-composed...
| |
by: David Pratt |
last post by:
Hi. I am splitting a string on a non whitespace character. One or more
whitespace characters can be returned as items in the list. I do not
want the items in the list that are only whitespace (can be one or more
characters of whitespace) and plan to use string.strip on those items
that are not only whitespace (to remove any whitespace from...
|
by: John Salerno |
last post by:
Forgive my excitement, especially if you are already aware of this, but
this seems like the kind of feature that is easily overlooked (yet could
be very useful):
Both 8-bit and Unicode strings have new partition(sep) and
rpartition(sep) methods that simplify a common use case.
The find(S) method is often used to get an index which is then...
|
by: pengb |
last post by:
Hello All:
A pretty frustrating question to ask! I use the string something
= stringx.split (delimiter.tochararray()) mathod to delimit a long
string by space! So supposely
if the stringx is :
" something something1 something2 " I should get string =
something, string = something1...etc
However question comes what happens if the string...
|
by: kunaltilak |
last post by:
hello sir,
please help me out for a asp.net(c#) problem.
actually i want to make an online test series.
for that i want to paste each question with options into rich textbox.
each question has 4 options(named: a. , b. , c. , d. ).
like this:
Question:
f is defined on by
|
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...
|
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. ...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
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...
|
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...
|
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...
|
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...
| |
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...
| |