Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

UrlCombine Function - .NET Framework

RSS
Modified on Mon, Mar 22, 2021, 7:53 AM by Administrator Categorized as ·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;
    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.