DEV Community

dev.to staff
dev.to staff

Posted on

7 3

Daily Challenge #305 - Remove Anchors from URLs

Complete the function/method so that it returns the url with anything after the anchor (#) removed.

Examples

remove_url_anchor('dev.to#about')
returns 'dev.to'

remove_url_anchor('www.telegraph.co.uk/branding')
returns 'www.telegraph.co.uk/branding'

Tests

remove_url_anchor('www.twitter.com?page=1')
remove_url_anchor('www.twitter.com#about')

Good luck!


This challenge comes from jhoffner on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (13)

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
 
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
 
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
 
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
 
boris profile image
Boris Quiroz

Python:

def remove_url_anchor(str):
    print(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

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay