Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

parsing a string with automated tools

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
ba272
Asked:
ba272
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Are the "fields" in the string fixed width?
0
 
ba272Author Commented:
yes
0
 
Mohamed ZedanCommented:
could you give the fields description ....
      field1           field2
|---5chars---||--2Chars--|

etc... this would ease the job :)
0
 
Fernando SotoCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now