Search code examples
stringclojuresplitseparator

Clojure - Split String without loosing the separator


Does Clojure have a Split function that splits the String into sub-strings including the separator? Like "a=b", separator "=" return: "a", "=", 'b". thanks!


Solution

  • i find regexp to be the simplest variant:

    user> (re-seq #"[^=]+|=" "asd=dfg=hgf=jjj")
    ;;=> ("asd" "=" "dfg" "=" "hgf" "=" "jjj")
    
    user> (re-seq #"[^=]+|=" "asd=dfg=hgf=")
    ;;=> ("asd" "=" "dfg" "=" "hgf" "=")
    
    user> (re-seq #"[^=]+|=" "=dfg=hgf=dffff")
    ;;=> ("=" "dfg" "=" "hgf" "=" "dffff")