Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String.Split Method (Char()) problem

Posted on 2012-03-12
11
Medium Priority
?
301 Views
Last Modified: 2012-03-18
We download a file which can be tab or comma delimited format.

We then split it in c# and handle the data.

Some fields contain commas(,) and some contain tabs.

How would you handle not making a mistaken Split on the data or know when the next record is the real one  ?

Sample , delimited:

101,Penthouse ...Corner...Directly on intracoastal, with three BRs, 145 ne 1 ave, etcetc

intracoastal, -> its not the correct split
0
Comment
Question by:goodluck11
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
  • +1
11 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 37712915
If a file is already created  ,honestly, there is no way. I'm talking about the case when comma is a delimeter and values may contain commas. All commas are the same.

One of common ways is to escape commas when creating files, see http://stackoverflow.com/questions/769621/dealing-with-commas-in-a-csv-file .

Another way is to put comma-containing values between quotation marks. However this rises other issues (how to handle quotation marks...).
0
 
LVL 40
ID: 37713260
This file does not follow the conventions of a delimited format file.

The delimiter should be a character or combination of characters that cannot be part of the data, so you need to control the thing at the point where the file is created.

Note that tabs and comas are used most of the time because they fit most uses. But you can use any character. Once again, this has to be defined at the point where the file is created.

You could use any of the following formats in which the separators are respectively #, "," and xyz.

101#Penthouse ...Corner...Directly on intracoastal, with three BRs# 145 ne 1 ave# etcetc
101","Penthouse ...Corner...Directly on intracoastal, with three BRs"," 145 ne 1 ave"," etcetc
101xyzPenthouse ...Corner...Directly on intracoastal, with three BRsxyz 145 ne 1 avexyz etcetc
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 37713482
This could be done if some rules exist on how the record is constructed.

For example:
 - first field is three characters (as shown)
 - second field is minimum twenty characters (as shown)
 - third field contains between one and five digits (not shown)
 - fourth field can be empty or will have both characters and digits up to four, and
     characters will be uppercase (not shown)
 - etc.

/gustav
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:goodluck11
ID: 37717593
How come when we open the file on excel, excel is able to separate the columns correctly for , commas and tabs

how can we do that in c# ?
0
 

Author Comment

by:goodluck11
ID: 37717651
so we have this code, and we are going to call it from protected void Page_Load(object sender, EventArgs e)
        {

 where exactly  do we put it ? Error      1      Extension method must be defined in a non-generic static class      C:\Users\Documents\Visual Studio 2010\Projects\Default.aspx.cs      21      22      


 public static string[] SplitWithQualifier(this string text,
                                             char delimiter,
                                             char qualifier,
                                             bool stripQualifierFromResult)
    {
        string pattern = string.Format(
            @"{0}(?=(?:[^{1}]*{1}[^{1}]*{1})*(?![^{1}]*{1}))",
            Regex.Escape(delimiter.ToString()),
            Regex.Escape(qualifier.ToString())
        );

        string[] split = Regex.Split(text, pattern);

        if (stripQualifierFromResult)
            return split.Select(s => s.Trim().Trim(qualifier)).ToArray();
        else
            return split;
    }
0
 

Accepted Solution

by:
goodluck11 earned 0 total points
ID: 37718020
the solution:


    public static class Class1
    {

        public static string[] SplitWithQualifier(this string text,
                                                     char delimiter,
                                                     char qualifier,
                                                     bool stripQualifierFromResult)
        {
            string pattern = string.Format(
                @"{0}(?=(?:[^{1}]*{1}[^{1}]*{1})*(?![^{1}]*{1}))",
                Regex.Escape(delimiter.ToString()),
                Regex.Escape(qualifier.ToString())
            );

            string[] split = Regex.Split(text, pattern);

            if (stripQualifierFromResult)
                return split.Select(s => s.Trim().Trim(qualifier)).ToArray();
            else
                return split;
        }
    }
0
 
LVL 52

Expert Comment

by:Gustav Brock
ID: 37718605
> How come when we open the file on excel, excel is able to
> separate the columns correctly for , commas and tabs ..

That is not possible with the sample you provided:

> 101,Penthouse ...Corner...Directly on intracoastal, with three BRs, 145 ne 1 ave, etcetc

But thanks for your solution tested with your secret test data.

/gustav
0
 

Author Comment

by:goodluck11
ID: 37727396
secret test data

601,140 NE 28 AVE # 601,POMPANO,33062,"Penthouse ...Corner...Directly on intracoastal, beautiful view of the intracoastal and ocean, nice building, walking distance to beach and food stores, restos etc....SPECIAL PRICE FROM MAY TO NOVEMBER $2,500 or Best Offer...LETS MAKE A DEAL....Short term ","3,500",2,2,0,1100,"Penthouse corner unit ...Directly on intracoastal,Walking distance to beach, Fantastic view from both sides,close to resto, food store, etc....",F738942,16,pompano yacht & beach clu
PH F,1390 S OCEAN BL # PH F,POMPANO,33062,"SPECTACULAR FOREVER OCEAN VIEW FROM THIS SE CORNER PENTHOUSE. BUILDING DIRECTLY ON THE SAND. EAT-IN-KITCHEN, NEW CARPET IN BEDROOMS, MARBLE IN FOYER, LIVING AREA, 3 BALCONIES (EAST, SOUTH AND WEST), NEARLY 3000 SQ. FT. 2 GARAGE PARKING SPACES. SEMI-PRIVAT","3,500",3,3,0,0,SPECTACULAR FOREVER OCEAN VIEW FROM THIS SE CORNER PENTHOUSE. GARAGE PARKING. SEMI PRIVATE ELEV. 24 HR SEC. FEELS LIKE A HOME IN THE SKY,F1008585,14,THE WITTINGTON CONDO
0
 
LVL 40
ID: 37727568
The best way to make people want to help you is to let them work for free for you and then end up with a stupid "secret" and not giving the points ot anybody.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 37727734
"The best way to make people NOT want to help ..." -  ha? :)
0
 

Author Closing Comment

by:goodluck11
ID: 37734454
solution found
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question