Solved

parsing a string with automated tools

Posted on 2006-10-21
4
259 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
[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
4 Comments
 
LVL 86

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

751 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