0

我在 clojure 中创建字符串时遇到问题,我需要根据其中一个字符串变量的长度插入一系列空格 - 书。

(defn blanks [book]
    (let [ x(count book)]
    (cond 
            (= "4" x) "    "
            (= "5" x) "     "
            (= "6" x ) "      "
)))

(defn Key1 [x date book bookid]
    (cond 
            (= "AB.LN.TUV" x) (str date".                  AB.LN.  TUV. JKL.      FOO.           FOO.   GRAVITY.       "book".book."(str (blanks book)) bookid)
            (= "DEF.NY.ZXY" x) (str date". DEF.NY. ZXY. JKL.     .          .QPR.             "book".  POS.        "book"."bookid)
            (= "DEF.LN.TUV" x ) (str date".                DEF.LN.  TUV. JKL.      FOO.           FOO.   GRAVITY.       "book".book."blanks bookid)
))



(defn ShowSelectedParams [& props] 
    (let [
              entity "AB.LN.TUV"


              book "ABCD"
              date "21030823"
              bookid "1234abcd"
              ]

              (Key1 entity date book bookid)
))

但是,这将返回以下内容,但不包含空格:

"21030823. AB.LN. TUV. JKL. FOO. FOO. GRAVITY. ABCD. book.1234abcd "

我想要得到的是:

“21030823. AB.LN. TUV. JKL. FOO. FOO. GRAVITY. ABCD.*强文本*book. 1234abcd”

对此非常感谢的任何帮助

4

1 回答 1

1

这是为了让它更地道一点

我对格式应用了更标准的 clojure 样式,根据计数而不是对字符串进行硬编码来生成空格的数量,并用 case 语句替换了重复的相等性检查。

(defn blanks
  [book]
  (apply str (repeat (count book) \space)))


(defn Key1
  [x date book bookid]
  (case x
    "AB.LN.TUV" (str date
                     ".AB.LN.TUV.JKL.FOO.FOO.GRAVITY."
                     book ".book."
                     (blanks book)
                     bookid)
    "DEF.NY.ZXY" (str date
                      ".DEF.NY.ZXY.JKL...QPR."
                      book
                      ".POS."
                      book
                      "."
                      bookid)
    "DEF.LN.TUV" (str date
                      ".DEF.LN.TUV.JKL.FOO.FOO.GRAVITY."
                      book
                      ".book."
                      blanks
                      bookid)))

(defn ShowSelectedParams
  [& props] 
  (let [entity "AB.LN.TUV"
        book "ABCD"
        date "21030823"
        bookid "1234abcd"]
    (Key1 entity date book bookid)))
于 2013-08-23T14:32:58.690 回答