gpt4 book ai didi

regex - 将连字符字符串转换为 CamelCase

转载 作者:行者123 更新时间:2023-12-02 23:24:48 25 4
gpt4 key购买 nike

我正在尝试将连字符字符串转换为 CamelCase 字符串。我关注了这篇文章:Convert hyphens to camel case (camelCase)

(defn hyphenated-name-to-camel-case-name [^String method-name]
(clojure.string/replace method-name #"-(\w)"
#(clojure.string/upper-case (first %1))))


(hyphenated-name-to-camel-case-name "do-get-or-post")
==> do-Get-Or-Post

为什么我的输出字符串中仍然出现破折号?

最佳答案

您应该将first替换为second:

(defn hyphenated-name-to-camel-case-name [^String method-name]
(clojure.string/replace method-name #"-(\w)"
#(clojure.string/upper-case (second %1))))

您可以通过在代码中插入 println 来检查 clojure.string/upper-case 获取的参数:

(defn hyphenated-name-to-camel-case-name [^String method-name]
(clojure.string/replace method-name #"-(\w)"
#(clojure.string/upper-case
(do
(println %1)
(first %1)))))

运行上面的代码,结果是:

[-g g]
[-o o]
[-p p]

向量的第一个元素是匹配的字符串,第二个元素是捕获的字符串,这意味着您应该使用第二,而不是第一

关于regex - 将连字符字符串转换为 CamelCase,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17138518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com