c# - Splitting string into smaller strings


Keywords:c# 


Question: 

I have a following list of names:

List<string> names = new List<string>();
names.Add("Tom Example");
names.Add("Ron Example");
names.Add("Peter Pavel Example");
names.Add("Pavel Ron Tom Example");

I want to split string in smaller strings for each element of list:

string firstName;
string firstInitial;
string lastName;
string lastInitial;
string secondName;
string secondInitial;

I want do this method in loop becouse I want to do some things on that small strings. And if I have 3 names and surname in string I want to have only first and second name and surname. For clearance:

For last element of list I want to have that result:

string firstName == Pavel;
string firstInitial == P;
string lastName == Example;
string lastInitial == E;
string secondName == Ron;
string secondInitial == R;

I have no idea how to implement this problem to work correctly. Any ideas?


1 Answer: 

I've prepared following solution for yourself:

public enum NameOptions
{
    FirstName = 1,
    MiddleName = 2,
    LastName = 4
};

Then extensions:

public static string SafeTokenPosition(this string @string, int index)
{
    if (string.IsNullOrEmpty(@string))
    {
        return string.Empty;
    }

    var tokens = @string.Split(new[] { " " }, 
                               StringSplitOptions.RemoveEmptyEntries);
    return index >= tokens.Length ? string.Empty : tokens[index];
}

private static string ResolveLastName(string name)
{
    if (string.IsNullOrWhiteSpace(name))
    {
        return string.Empty;
    }

    var tokens = name.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

    switch (tokens.Length)
    {
        case 2:
            return tokens[1];
        case 3:
            return tokens[2];
        default:
            return string.Empty;
    }
}

private static string ResolveMiddleName(string name)
{
    if (string.IsNullOrWhiteSpace(name))
    {
        return string.Empty;
    }

    var tokens = name.Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

    switch (tokens.Length)
    {
        // ex. John Pablo Kowalsky
        case 3:
            return tokens[1];
        default:
            return string.Empty;
    }
}

private static string ResolveFirstName(string name)
{
    return name.SafeTokenPosition(0);
}

And final extensions:

public static string ExtractName(this string @string, NameOptions nameOptions)
{

    switch (nameOptions)
    {
        case NameOptions.FirstName:
            return ResolveFirstName(@string);
        case NameOptions.MiddleName:
            return ResolveMiddleName(@string);
        case NameOptions.LastName:
            return ResolveLastName(@string);
        default:
            throw new NotSupportedException("Name " + nameOptions + " is not supported.");
    }
}

usage:

var firstName = "John Sample".ExtractName(NameOptions.FirstName);

It should be easily to extend to handle initial letters and two - seconds names.