Combine overlapping URLs in C #

I have two urls. The server and relative URL that I would like to combine. The problem is that part of the URL may overlap. I did this using some terrible string manipulations, but would like to put it there and see if there is a good and clean way to do this.

string siteUrl = "http://seed-dev6/sites/irs";
string formUrl = "/sites/irs/Forms/testform.xsn";
+3
source share
4 answers

I would separate the URLs based on their path separator /, combine the lists without duplicates while maintaining order, and then combine them into one URL string.

, . , ( ), - %20 ..

+3

Knuth-Morris-Pratt . , :

overlap[0] = -1;
for (int i = 0; pattern[i] != '\0'; i++) {
overlap[i + 1] = overlap[i] + 1;
while (overlap[i + 1] > 0 &&
       pattern[i] != pattern[overlap[i + 1] - 1])
    overlap[i + 1] = overlap[overlap[i + 1] - 1] + 1;
}
return overlap;

#, ( ) .

+2
    string siteUrl = "http://seed-dev6/sites/irs";
    string formUrl = "/sites/irs/Forms/testform.xsn";

    string result = siteUrl + formUrl;
    for (int n = 1; n < formUrl.Length; ++n)
    {
        if (siteUrl.EndsWith(formUrl.Substring(0, n)))
            result = siteUrl + formUrl.Substring(n);
    }

    return result;
0
source

That should do the trick. It is also possible to use Path.DirectorySeparatorChar instead of '/'.

char delim = '/';
string siteUrl = "http://seed-dev6/sites/irs";
string formUrl = "/sites/irs/Forms/testform.xsn";
string fullUrl = string.Join(
    new string(delim,1), 
    siteUrl.Split(delim).Concat(formUrl.Split(delim)).Distinct().ToArray());
0
source

Source: https://habr.com/ru/post/1739174/


All Articles