【今日湯底】
Complete the function/method so that it returns the url with anything after the anchor (#) removed.
Examples
"www.codewars.com#about" --> "www.codewars.com"
"www.codewars.com?page=1" -->"www.codewars.com?page=1"
(必須通過以下測試)
(ns kata.test
(:require [clojure.test :refer :all]
[kata :refer [remove-url-anchor]]))
(deftest basic-tests
(is (= (remove-url-anchor "www.codewars.com#about") "www.codewars.com"))
(is (= (remove-url-anchor "www.codewars.com/katas/?page=1#about") "www.codewars.com/katas/?page=1"))
(is (= (remove-url-anchor "www.codewars.com/katas/") "www.codewars.com/katas/")))
【我的答案】
(ns kata)
(defn remove-url-anchor [url]
(first (clojure.string/split url #"#"))
)
【其他人的答案】
(ns kata)
(defn remove-url-anchor [url]
(clojure.string/replace url #"#.*" "")
)
(ns kata)
(defn remove-url-anchor [url]
(if (= -1 (.indexOf url "#"))
(subs url 0)
(subs url 0 (.indexOf url "#"))
)
)