Solved

parsing a string with automated tools

Posted on 2006-10-21
4
250 Views
Last Modified: 2010-04-16
Greetings,

I have a string to parse which might fit either of these two models.

 CA LOS ANGELES  AFTON PL          6100-6299    8131        20    90028  
                            ALCYONA DR      2100-2399    8131        11    90068  

I need to easily parse this to fill:

string state;
string city;
string street;

but I want to design the approach to be a bit flexible in the event I need to change it.
So I think using automated tools would be the preferred method.

Can anybody suggest how to do this?

Bob
What's the easiest way


0
Comment
Question by:ba272
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17780916
Are the "fields" in the string fixed width?
0
 

Author Comment

by:ba272
ID: 17780924
yes
0
 
LVL 7

Expert Comment

by:Mohamed Zedan
ID: 17783242
could you give the fields description ....
      field1           field2
|---5chars---||--2Chars--|

etc... this would ease the job :)
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 17784024
Hi Bob;

Below is sample code to do what you want.

            string[] TestData = {
                "CA LOS ANGELES  AFTON PL          6100-6299    8131        20    90028",
                "                ALCYONA DR        2100-2399    8131        11    90068"
            };
            string State;
            string City;
            string Street;

            // In this code sample I am assuming the following
            //     State is in position index  0-1
            //     City is in position index   3-14
            //     Street is in position index 16-32
            // The first and second parameter of SubString will need to be adjusted
            // to meet the acual field start index and length
            foreach (string data in TestData)
            {
                // Parse the data
                State = data.Substring(0, 2).Trim();
                if (State.Length == 0) State = "<Empty State Field>";
                City = data.Substring(3, 12).Trim();
                if (City.Length == 0) City = "<Empty City Field>";
                Street = data.Substring(16, 17).Trim();
                if (Street.Length == 0) Street = "<Empty Street Field>";
                // Use the parsed data as needed
                MessageBox.Show(State + "\n" + City + "\n" + Street);
            }


Fernando
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is "public object" in C#? 3 65
Programmatically storing deadlock graphs with SQL Server 6 52
how to remove duplicate code from my project 5 35
linq, c# 8 24
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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

831 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