Skip to main content

The ReformatDisplayName method rearranges a display name from "LastName FirstName" format to "FirstName LastName" format. It handles edge cases like middle names or initials, properly repositioning them and removing any extra whitespace.

public static string ReformatDisplayName(string displayName)
{
    if (string.IsNullOrWhiteSpace(displayName))
    {
        return displayName;
    }

    // Trim leading and trailing spaces
    displayName = displayName.Trim();

    int firstSpaceIndex = displayName.IndexOf(' ');

    if (firstSpaceIndex == -1)
    {
        // Only one name, return as is
        return displayName;
    }

    // Extract last name and the rest (first and middle names)
    string lastName = displayName.Substring(0, firstSpaceIndex);
    string firstAndMiddleNames = displayName.Substring(firstSpaceIndex + 1);

    // Reconstruct the name in "First Middle Last" format
    return firstAndMiddleNames + " " + lastName;
}

//
// Unit tests
//

using System;
using Xunit;

public class ReformatDisplayNameTests
{
    [Fact]
    public void ReformatDisplayName_NullInput_ReturnsNull()
    {
        string displayName = null;

        string result = ReformatDisplayName(displayName);

        Assert.Null(result);
    }

    [Fact]
    public void ReformatDisplayName_EmptyString_ReturnsEmpty()
    {
        string displayName = "";

        string result = ReformatDisplayName(displayName);

        Assert.Equal("", result);
    }

    [Fact]
    public void ReformatDisplayName_WhitespaceString_ReturnsWhitespace()
    {
        string displayName = "   ";

        string result = ReformatDisplayName(displayName);

        Assert.Equal("   ", result);
    }

    [Fact]
    public void ReformatDisplayName_SingleWord_ReturnsSame()
    {
        string displayName = "Smith";

        string result = ReformatDisplayName(displayName);

        Assert.Equal("Smith", result);
    }

    [Fact]
    public void ReformatDisplayName_TwoWords_SwapsNames()
    {
        string displayName = "Smith John";

        string result = ReformatDisplayName(displayName);

        Assert.Equal("John Smith", result);
    }

    [Fact]
    public void ReformatDisplayName_MultipleWords_SwapsNames()
    {
        string displayName = "Smith John Michael";

        string result = ReformatDisplayName(displayName);

        Assert.Equal("John Michael Smith", result);
    }

    [Fact]
    public void ReformatDisplayName_LeadingTrailingSpaces_TrimsAndSwaps()
    {
        string displayName = "  Smith John  ";

        string result = ReformatDisplayName(displayName);

        Assert.Equal("John Smith", result);
    }

    [Fact]
    public void ReformatDisplayName_MultipleSpacesBetweenWords_HandlesCorrectly()
    {
        string displayName = "Smith   John";

        string result = ReformatDisplayName(displayName);

        Assert.Equal("John Smith", result);
    }

    [Fact]
    public void ReformatDisplayName_MiddleName_IncludesMiddleName()
    {
        string displayName = "Smith John A.";

        string result = ReformatDisplayName(displayName);

        Assert.Equal("John A. Smith", result);
    }

    [Fact]
    public void ReformatDisplayName_MiddleInitials_IncludesInitials()
    {
        string displayName = "Smith John A B";

        string result = ReformatDisplayName(displayName);

        Assert.Equal("John A B Smith", result);
    }

    [Fact]
    public void ReformatDisplayName_OnlySpaces_ReturnsSame()
    {
        string displayName = "   ";

        string result = ReformatDisplayName(displayName);

        Assert.Equal("   ", result);
    }
}