.NET Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgramming.NET Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old June 10th, 2003, 01:13 AM
jl39775 jl39775 is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: May 2003
Posts: 4 jl39775 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Splitting a csv file w/commas in a field

I have a comma delimited file with fields that have commas. I don't know how to get the split function form splitting when a field has a comma in it. I tried to put quotes around string values but the split function still splits into an element in an array.<br><br>my code<br><br>strInput="123,testdata,testdata with a comma ,,last field"<br>arrArray = strInput.Split(",")<br><br>what I want is:<br>arrArray(0)=123<br>arrArray(1)=testdata<br>arrArray(2)=testdata with a comma ,<br>arrArray(3)=last field<br><br>what I get is:<br>arrArray(0)=123<br>arrArray(1)=testdata<br>arrArray(2)=testdata with a comma <br>arrArray(3)=<br>arrArray(4)=last field<br><br>Can someone help?<br><br>Thanks,<br><br>James

Reply With Quote
  #2  
Old August 5th, 2003, 02:29 PM
LeonJollans LeonJollans is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 27 LeonJollans User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
If you hadn't said that the comma was part of the data item, there would have been no way of us knowing. How do you expect a program to know? Try delimiting your file with something else.

If you want to put the value in quotes to get around it, you'll need to parse the data manually and set a flag when you are inside a quoted block. eg in nearly pseudocode
Code:
ArrayList table = new ArrayList();
foreach ( string row in the_rows ){

    ArrayList thisrow = new ArrayList();
    bool inQuotes = false;
    int lastCommaIndex = 0;

    for( int i = 0; i< row.length; i++ ){
        if(row.substring(i,1) == "\""){
            inQuotes = ! inQuotes;
        }
        if( row.substring(i,1) == "," && !inQuotes){
            thisrow.Add(  row.substring( lastCommaIndex, i-lastCommaIndex ));
            lastCommaIndex = i;
        }
    }
    table.Add((string[])thisRow.ToArray(typeof(string)));
}
return (string[][])table.ToArray(typeof(string[]));




it's untested but that'll (crudely) give you a 2d array representing each item.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgramming.NET Development > Splitting a csv file w/commas in a field


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT