gpt4 book ai didi

regex - 在 R 中将字符转换为 lowerCamelCase

转载 作者:行者123 更新时间:2023-12-02 05:26:49 26 4
gpt4 key购买 nike

我有如下所示的字符向量:

x <- c("cult", "brother sister relationship", "word title")

我想将其转换为如下所示的 lowerCamelCase 样式:

c("cult", "brotherSisterRelationship", "wordTitle")

我玩过 gsubgregexprstrplitregmatches 和许多其他函数,但不能'抓不住。

尤其是一个字符中的两个空格似乎很难处理。

也许这里有人知道如何做到这一点。

最佳答案

> x <- c("cult", "brother sister relationship", "word title")
> gsub(" ([^ ])", "\\U\\1", x, perl=TRUE)
[1] "cult" "brotherSisterRelationship"
[3] "wordTitle"

引自pattern matching and replacement :

For perl = TRUE only, it can also contain "\U" or "\L" to convert the rest of the replacement to upper or lower case and "\E" to end case conversion.

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

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