Click here to Skip to main content
16,012,153 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi all, I am trying to split this row of values:
"databasename","DBAName","MerchantNumber","Status","Gateway","ShoppingCart","WebHost","HostingCoLocation","PaymentAppName","PaymentAppVersion","PaymentAppLastValidated","Phone","Registered for Scan","Scanner","ScanDate","ScanStaus","Next Scan Scheduled","SAQType","SAQGroup","SAQ Version","Compliant","dateconfirmed","AttestationName"

I used:
Dim strColumns As String() = row.Split(",")

This returns:
strColumns(0)="databasename"
            strColumns(1)="DBAName"....etc...

But I need:
strColumns(0)= databasename


How do i achieve this?

Please help.

Thanks.
Posted
Updated 21-Mar-13 9:10am
v3
Comments
[no name] 21-Mar-13 15:10pm    
It does not much sense what it is that you are asking (converting a string in a string array to be something other than a string but still keep it in a string array) but you *could* split your string by more than one character....

I guess the problem is extra " characters.

You need row.Split(new char[] {'"', ',', }, System.StringSplitOptions.RemoveEmptyEntries); that's all.

—SA
 
Share this answer
 
Comments
Maciej Los 21-Mar-13 15:13pm    
Short and to the point, +5!
Sergey Alexandrovich Kryukov 21-Mar-13 15:17pm    
Thank you, Maciej.
—SA
vidkaat 21-Mar-13 15:21pm    
Thank you so much for ur help.
Sergey Alexandrovich Kryukov 21-Mar-13 15:27pm    
Sure. Good luck, call again.
—SA
vidkaat 21-Mar-13 15:33pm    
When i do this i get an issue....My row length is 23 ...After splitting it changes to 16 i dont want to remove any empty entries...I tried even this
Dim strColumns As String() = row.Split(New Char() {""""c, ","c}, System.StringSplitOptions.None

But it didnt work..Please help.
While Sergey's solution will work fine for the specific example you show, I suspect it will cause problems latter on. The data fragment you have posted looks like basic CSV, (or comma separated values) data, and if so, then the use of quoted strings is deliberate: it is there so that commas can be included in string entries:
"abc", "def,ghi", "jkl"
Is three entries, not four:
abc
def,ghi
jkl
While you could use Sergeys solution, (or just use string.Replace("\"", "") to remove them all first) I would recommend that you use a CSV aware processor instead.
There are a number of ways to help you do just that:
A Fast CSV Reader[^]

C# - CSV Import Export[^]

Reading and Writing CSV Files in C#[^]
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 21-Mar-13 15:28pm    
You are describing a pretty difficult more general case. It's not the case in OP's sample, but the solution is good to know, my 5.
—SA
Sergey Alexandrovich Kryukov 21-Mar-13 15:29pm    
If you don't mind, I fixed punctuation in first 3 words...
—SA
OriginalGriff 21-Mar-13 15:48pm    
Not in the least! :laugh:
I'm wary of recommending a "simple" solution when you deal with quoted data - there's always something that comes along and bites you later! ;)
Sergey Alexandrovich Kryukov 21-Mar-13 16:04pm    
You are right. There is one difficult example: how to simulate pre-processing of the command line? I found that the processing is sophisticated and kind of unpredictable when " are used (important in file names with blank spaces). For example, when you violate balance of quotation marks, the system tries to fix it by doing some guesswork, and then the set of parameter strings is kind of unpredictable. I only used it to accelerate testing, but someone reported by simulation as a bug. (I simply replied that I'm not taking responsibility for simulation of undocumented behavior which is not a part of my production code.) Still, very unpleasant.

On a general note, a processing of formally invalid user input is very difficult and often does not make sense (say, in formal languages). In particular, that's why the automatic translation quality is ridiculous.

—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900