UrlCombine Function - .NET Framework

Overview

This article provides code for the UrlCombine function, which is similar to the System.IO.Path.Combine function but with forward slashes for a URL

Code

public static string UrlCombine(params string[] paths)
{
    var prefix = NormalizeUrlSlash(paths[0].Substring(0, 1));            

    var suffix = NormalizeUrlSlash(paths[paths.Length - 1].Substring(paths.Length - 1, 1));

    for (var i = 0; i < paths.Length; i++)
    {
        if (paths[i].StartsWith(_back) || paths[i].StartsWith(_fwd))
        {
            paths[i] = paths[i].Substring(1);
        }

        if (paths[i].EndsWith(_back) || paths[i].EndsWith(_fwd))
        {
            paths[i] = paths[i].Substring(0, paths[i].Length - 1);
        }
    }

    var result = prefix + string.Join(_fwd, paths) + suffix;

    return result;

    static string NormalizeUrlSlash(string input)
    {
        if (input == _back)
        {
            input = _fwd;
        }
        else if (input != _fwd)
        {
            input = string.Empty;
        }
        return input;
    }
}