Solved

parsing a string with automated tools

Posted on 2006-10-21
4
219 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
Comment Utility
Are the "fields" in the string fixed width?
0
 

Author Comment

by:ba272
Comment Utility
yes
0
 
LVL 7

Expert Comment

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

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

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now