0
votes

I need to change a string that contains a name in the "First Last" format to a "Last, First" format. The problem is, the string may be like "John Smith", or it may be "Mr. John Smith" or even "Mr. John A. Smith". How can I eliminate the "Mr." part and the middle name/initial part, and then make it "Smith, John"? I hate strings btw...I foresee a lot of Trim(Right), Trim(Left) crap... please make it easier!

My goal is to select records from the database field NAME by the first letter of the last name. The field NAME contains names like "John Smith" "Mr. Roger A. Smith" "Mr. Jones" etc. I just need a search function that returns the Smith's but not the Jones.

2
Uncle Bob has a screencast on TDD that deals specifically with this requirement and how to write tests that satisfy it. It covers salutations and such. I can't for the life of me remember which one it is though! - Simon Whitehead
There may not be a complete answer to that, without specific cultural knowledge - for example, if someone had the name "Jean-Claude Van Damme", the correct answer might be "Van Damme, Jean-Claude" (as in the case of the director) but could also be "Damme, Jean-Claude" if there is a culture with "Van" as a valid middle name. There are also cultures where the surname comes first, e.g. China. There is no "rule". One of my ancestors had a surname that looked (in the relevant language) almost exactly like "Miss Mans". - AMADANON Inc.
Also, in Dutch, a name like JCVD's would be listed as "van Damme, Jeann-Claude", under the letter "D" in the phonebook (note also the lowercase "v" in "van". Another common prefix is "van der" - as in "van der Graaf generator" - this is sometimes abbreviated to "v/d Graaf" - AMADANON Inc.
Who's Uncle Bob? And I'm going to guess that the Jean-Claude issue won't come up much as it's US based program with mostly American names. - JimB
How that string is formed? Is it a user input? - Tushar

2 Answers

1
votes

Does this fulfill your requirements?

Dim samples As String() = {
    "John Smith",
    "John A. Smith",
    "Mr. John Smith",
    "Mr. John A. Smith",
    "Jean-Claude Van Damme"
}

Dim regex As New Regex("(?:(?:mr\.|miss|mrs|ms)\s+)?(\S+).*(?<=\s)(\S+)$", RegexOptions.IgnoreCase)

For Each sample In samples
    Dim converted As String = regex.Replace(sample, "$2, $1")
    Console.WriteLine("{0} => {1}", sample, converted)
Next

Results:

John Smith => Smith, John
John A. Smith => Smith, John
Mr. John Smith => Smith, John
Mr. John A. Smith => Smith, John
Jean-Claude Van Damme => Damme, Jean-Claude

.Net Fiddle

-1
votes

I haven't worked much with vb.net but this code should work in any language once modified to use the correct function names

#include <string>

using namespace std;

int main()
{
    string name;

    unsigned int startIndex = 0;
    for(unsigned int i = name.length() - 1; name[i] != ' '; --i)
    {
        startIndex = i;
    }

    unsigned int LengthOfName = name.length();
    string sirName = name.substr(startIndex, LengthOfName);

    startIndex = 0;
    if(name[0] == 'm' || name[0] == 'M')
    {
        for(unsigned int i = 0; name[i] != ' '; ++i)
        {
            startIndex = i + 1;
        }
    }

    for(unsigned int i = startIndex; name[i] != ' '; ++i)
    {
        LengthOfName = i - startIndex;
    }

    string firstName = name.substr(startIndex, LengthOfName);

    name = sirName + ' ' + firstName;

    return 0;
 }

--Code written in c++