Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 02-03-2011, 01:34 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: Baton Rouge, LA
Posts: 131
j3rm is on a distinguished road
Default Efficent Parsing of Name prefixes/sufixes

Hi,
I would like to take a string that has a value of "Dr. Bob A. Smith, II"
and Get "Bob A. Smith".
I have a NSArray of Name Prefixes, suffixes that I want to scan the Full Name for and remove them.
I have looked into NSScanner but I don't see a clear efficient way to do what i want to short of looping through the prefix/suffix and removing them from the string.

Is there a better way?
Thanks for your help
j3rm is offline   Reply With Quote
Old 02-03-2011, 02:18 PM   #2 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
Join Date: Aug 2008
Location: Memphis, TN, USA
Age: 24
Posts: 3,983
smithdale87 is on a distinguished road
Send a message via AIM to smithdale87
Default

I can think of some worse ways to do it, but none really come to mind as being considerably better. Are you doing this for a tremendous amount of data? Is that why you are looking for a really efficient method?
smithdale87 is offline   Reply With Quote
Old 02-03-2011, 02:33 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: Baton Rouge, LA
Posts: 131
j3rm is on a distinguished road
Default

I may need to run it against all contacts that are synced to my app ( not the device address book) from a CRM app, so it could be a couple hundred or couple thousand. I use a SectionKey field for the contact list and I cant have all the Dr.'s in the D section. So I will need to build these keys probably after every sync, and it could be killer if i had to do thousands.

So no bettter way...?
j3rm is offline   Reply With Quote
Old 02-03-2011, 03:41 PM   #4 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

Well, doing the same thing with low level C string functions would probably be faster. But is NSScanner really that slow that it's going to make a difference?
JasonR is offline   Reply With Quote
Old 02-03-2011, 04:48 PM   #5 (permalink)
Registered Member
 
Join Date: Jan 2010
Location: Baton Rouge, LA
Posts: 131
j3rm is on a distinguished road
Default

I'm not planning on using NSScanner, I don't see a clear way to accomplish what I am trying to do. I want to remove the strings, scanner seems to pull them into another string.

what I am proposing is a simple for loop, if object exist in string then replace it with nothing. that doesn't sound efficient, thus the reason for this post.

Is there a way to take a string "Dr. Bob Smith, II" and take my name prefix/suffix array and remove the spares so I get just the name.

I looked through the documentation on NSScanner but I don't see a clear way to do with out more for loops.

Thanks for your help.
j3rm is offline   Reply With Quote
Old 02-04-2011, 07:11 PM   #6 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

The one problem you will have is NSString's don't work like that. They don't allow you to delete parts of themselves, you have to convert them to NSMutableStrings to do that. I think instead it's better to just return a string without the title.

In your position, I would use the rangeOfString: method to find out if the string has the prefix, and use subStringFromIndex: to return the part of the name that does not include the title. Both of these are NSString methods.

I hope that helps.
JasonR is offline   Reply With Quote
Old 02-26-2011, 08:45 PM   #7 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 45
SoftmasterG is on a distinguished road
Default

"Dr. Bob A. Smith, II"

You could use componentsSepartedByString to create an array which would contain:

Dr.
Bob
A.
Smith,
II

You can then compare the first element to the array you have or you can take the array of Name Prefixes and create a string out of that and check to see if the first item is contained in the Name Prefix string. If you do it this way you have a single comparison whereas if you use the Name Prefix array you have n comparisions, where n = number of items in the Name Prefix array.
SoftmasterG is offline   Reply With Quote
Old 04-19-2011, 05:28 PM   #8 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,002
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by j3rm View Post
Hi,
I would like to take a string that has a value of "Dr. Bob A. Smith, II"
and Get "Bob A. Smith".
I have a NSArray of Name Prefixes, suffixes that I want to scan the Full Name for and remove them.
I have looked into NSScanner but I don't see a clear efficient way to do what i want to short of looping through the prefix/suffix and removing them from the string.

Is there a better way?
Thanks for your help
The most efficient way is probably to create arrays of unichars and write your own library of functions that will extract an array of prefixes and suffixes from your strings. That would be a fair amount of work, however, and you'd have to figure out how to handle your memory management.

You could also use a loop that would use rangeOfString to find the prefix, then the suffix, then calculate a new range and use substringWithRange to get the part you actually want. If you could set up your code to create mutable strings to start with, you could call deleteCharactersInRange: to remove the prefix string and suffix string separately. I don't know if that would be faster than creating a new, shorter NSString from the original NSString. You might need to try some tests using each approach.

No matter what you do, you've got an "N squared" problem. You need to compare all your source names against all your prefixes and all your suffixes, and replace most or all of your source strings. You'll be doing name_count * (prefix_count + suffix_count) comparisons, and creating shorter versions of potentially ALL of your names.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 405
17 members and 388 guests
Brandt, coolman, fredidf, Free App Monster, givensur, iAppDeveloper, jbro, Kryckter, locombiano89, Mah6447, Meoz, simplymuzik3, SLIC, stanny, stevenkik, Tomsky, WeaselPig
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,646
Threads: 94,111
Posts: 402,862
Top Poster: BrianSlick (7,990)
Welcome to our newest member, locombiano89
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:51 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0