I have a function that takes in a string s and a map of characters charmap. If any characters in the string s are inside of charmap, replace the character with the value of the map.
Note, the keys in the map must be a string, rather than a char.
For example:
(replace-characters "Hi!" {"!" "Exclamation Mark"}) ;; => "HiExclamation Mark"
This is the code I am currently using:
(defn- replace-characters
"Replaces any characters in a string that are mapped in the given charmap"
[s charmap]
(let [character-set (set s)
characters (filter #(contains? charmap (str %)) character-set)]
(string/replace s (re-pattern (string/join "|" characters)) charmap)))
However, I am getting a NullPointerException and I am seriously confused on why.
java.lang.NullPointerException: Cannot invoke "String.indexOf(int)" because "s" is null
I would like to solve this in pure Clojure preferably, not Java, etc.
Update: So the code above works in the repl. Which is nice. But for some reason the following is causing the error:
(-> s
(string/replace #"[:/?#\[\]@!$&'()*+,;=]" "")
(replace-characters charmap)) ;; Where charmap is a large map of key value characters.