DEV Community

Daily Challenge #305 - Remove Anchors from URLs

dev.to staff on November 10, 2020

Complete the function/method so that it returns the url with anything after the anchor (#) removed. Examples remove_url_anchor('dev.to#...
Collapse
 
cipharius profile image
Valts Liepiņš

Haskell:

remove_url_anchor :: String -> String remove_url_anchor = takeWhile (/= '#') 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
alfredosalzillo profile image
Alfredo Salzillo

So much beautiful to read.

Collapse
 
peter279k profile image
peter279k

Here is the simple solution with parse_url function in PHP:

function replaceAll($string) { $parsed = parse_url($string); $scheme = $parsed['scheme'] ?? false; if ($scheme !== false) { return $scheme . '://' . $parsed['host']; } return $parsed['path']; } 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
calinzbaenen profile image
Calin Baenen • Edited

Java:

class URL { private String url = null; public URL(String url) {this.url = url;} // The url. public String removeAnchor() { if(url != null) return url.split("#")[0]; // Get the part before the anchor. return ""; } } 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
danielt404 profile image
DanielT404

C# solution

class MainClass { public static string remove_url_anchor(string url) { int anchorFoundAt = url.IndexOf("#", 0, url.Length); if(anchorFoundAt == -1) return url; return url.Substring(0, anchorFoundAt); } public static void Main (string[] args) { string url = remove_url_anchor("dev.to#about"); Console.WriteLine(url); } } 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
qm3ster profile image
Mihail Malo • Edited

JavaScript

const remove_url_anchor = url => url.split('#', 1)[0] 
Enter fullscreen mode Exit fullscreen mode

Second parameter to split means it will stop looking after the first match, and never construct a second string value.

Rust

#![feature(str_split_once)] fn remove_url_anchor(url: &str) -> &str { url.split_once('#').map_or(url, |(x, _)| x) } 
Enter fullscreen mode Exit fullscreen mode

look at it go!

Disclaimer:

In real life, please use developer.mozilla.org/docs/Web/API... or docs.rs/url/ respectively.

These should be fairly bulletproof though, since URL specification doesn't allow unencoded # character anywhere, including notably the <password> section of <protocol>://<user>:<password>@<host>. But where there's fragment, soon will come other parts of the URL.

Collapse
 
mellen profile image
Matt Ellen-Tsivintzeli • Edited

REGEX to the rescue.

function remove_url_anchor(url) { return url.replace(/([^#]*)#.+/, '$1'); } 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sabbin profile image
Sabin Pandelovitch

js using split method

const remove_url_anchor = url => url.split('#')[0]; 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
jehielmartinez profile image
Jehiel Martinez

JS

removeUrlAnchor = (str) => str.split('#')[0]; 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rafi993 profile image
Rafi

SQL (postgres)

SELECT split_part('dev.to#about', '#', 1); 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
boris profile image
Boris Quiroz

Python:

def remove_url_anchor(str): print(str.split("#")[0]) 
Enter fullscreen mode Exit fullscreen mode
Collapse
 
annshuk profile image
Annshuk Sharma

const remove_url_anchor =(url) => url && url.split('#')[0];

Collapse
 
arepodesir profile image
Arepo Desir

Ha! I love the English like precision in syntax.