472,779 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

string.split question

Code
----------------------
string Line = "\"A\",\"B\",\"C\",\"D\"";

string Line2 = Line.Replace("\",\"","\"\",\"\"");

string [] CSVColumns = Line2.Split("\",\"".ToCharArray());

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
Nov 16 '05 #1
6 6296
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("\",\"".ToCharArray());
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.RegularExpressions.Regex.Split(Line2, "\",\"");
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(',');
Nov 16 '05 #2


"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("\",\"".ToCharArray());

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.
Nov 16 '05 #3
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***@discussions.microsoft.com> wrote in message
news:09**********************************@microsof t.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("\",\"".ToCharArray());

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.
Nov 16 '05 #4
Senthil,
In addition to the other comments.

There are three Split functions in .NET:

Use Microsoft.VisualBasic.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.Split if you need to split a string based on a collection
of specific characters. Each individual character is its own delimiter.

Use System.Text.RegularExpressions.RegEx.Split to split based
on matching patterns.
By referencing the Microsoft.VisualBasic assembly in C# you can use the
Strings.Split function to split a string based on a word.

Something like:
string [] CSVColumns = Strings.Split(Line2, "\",\"", -1,
CompareMethod.Binary);

Hope this helps
Jay
"Senthil" <se***********@yahoo.com> wrote in message
news:46*************************@posting.google.co m...
Code
----------------------
string Line = "\"A\",\"B\",\"C\",\"D\"";

string Line2 = Line.Replace("\",\"","\"\",\"\"");

string [] CSVColumns = Line2.Split("\",\"".ToCharArray());

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

Nov 16 '05 #5
"Jerry" <Je***@discussions.microsoft.com> wrote in message news:<09**********************************@microso ft.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("\",\"".ToCharArray());

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
Nov 16 '05 #6
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.google.co m...
"Jerry" <Je***@discussions.microsoft.com> wrote in message news:<09**********************************@microso ft.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("\",\"".ToCharArray());

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

Nov 16 '05 #7

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

Similar topics

2
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...
1
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...
5
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...
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...
8
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...
3
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...
25
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...
2
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 :...
0
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...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.