I am working on an application where I am presented a string that has some information, its always in the same order, but not always the same length with no special separators, here is an example

"May 30 10:08 am ARRIVAL AT UNIT WILMINGTON DE 19850"

I need to be able to take these string and separate them into 3 parts

Time = "May 30 10:08 am";
Status = "ARRIVAL AT UNIT";
Location = "WILMINGTON DE 19850";

the location will always be a city but might have a space in it like "SAN JOSE"
this is for a package tracking application, I hit a snag on this, just not sure how I should do it, or how even to do it.

I could loop through all the months and call contains, if its found, save it and remove it from the string. the get the next 12 characters and append it to month and have the time. I'm not sure if that's the best practice, but then I have no idea how to separate the status from the location.

ideas friends?

Recommended Answers

All 11 Replies

If status part is fixed then I'd suggest split a string at status.

none of them are fixed The order is always the same, but the length, start and end index all depend on the length of the status which could be any of several different statuses.

So you have three parts:
Some date
Some text (which according to you could be anything)
Some other text (which accroding to you could be anything)

You can split the date from the rest, that's not hard to do. But unless there is some format to the other two (something you can always count on) there is no way to split them.

This is but a suggestion:
You could perhaps first split your string and for every splitted string see if it is a prefix( SAN ST SAINT etc.) Great possibility that the following string is a city name.
If that doesn't work, look up for every splitted string in a huge table of city names, until you got a match. This will be a busy operation I guess, but perhaps threads can help here?
Once you got the city name, it will be easy to split the string the way you want I hope.

I thought about using regular expressions and look for "some letters" space "some letters" space 5numbers to get the location, but that's where the possibility of city names with spaces like those above, I'm not all that good at regular expressions is there a way to check for San, St, Saint ect optionally in an expression like that?

What I know of regex, this must be possible, but you still have the other cities.
Does your string always ends with 5 digits?

yes, its always a 5 digit us postal zip code.

Haha! So if you know the zip code, then to know the city must be peace of cake. Although the american ZIP codes seem a bit complicated at frst sight( to me that is: Belgium, we have a world record btw! :'( ) it should pose no insurmountable problem.
Work back in the string, the rest without the date string must be your status string.

our zip codes aren't that complicated :) well basically its a 5 digit number, but then again, they are able to be fined tuned down to a smaller area via another 5 digit number hyphenated onto that one, but only carriers seem to know where to get that information from, its not listed anywhere I've ever seen.

on a further note, I didn't even think about using the zip to find out what the city was. Hrm, I wonder if there is a API for that online or something.

I know they sell some DB's with cities and other info for all over the world.
Perhaps there is some free material?

the United States Post Service website has a web api for zipcode look up, so I guess I have all the information I need now, this is a very complicated solution, but it is a solution none the less, thank you for your help everyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.