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.
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?
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.
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?
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.
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.
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.
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.
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.