简体   繁体   中英

c# algorithm to check if a specific “word” exists in url

I have an url:

http://www.domain.com/path/path/path/path/file1.html

And then another one:

http://www.domain.com/path/path/external/path/path/file1.html

Please notice /external/ within the path.

Now the files "are the same" but they target different audience based on the /external/ . If the path contains /external/ then it's for external use, otherwise is for internal use.

My question how do I relate these 2 files to each other? The '/external/' can be at any location within the path.

Each file might have max one external type or none at all.

Now to map the path to it's external type I am thinking to do like this:

For a specific file:

  • targetPath = http://www.domain.com/path/path/path/path/file1.html
  • get all file paths that contains /external/ as a list
    • foreach path in list
      • remove /external/
      • if path == targetPath
        • external path for targetPath is path
      • break loop

Well, actually you don't have to add all the file paths which contain /external/ into a list. You could simply use the String.Replace method.

string targetPath = "http://www.domain.com/path/path/external/path/path/file1.html";
targetPath = targetPath.Replace("/external/", "/");

This will replace all /external/ parts in the string with a single / . And if the string doesn't contain any /external/ string, it simply replaces nothing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM