How to convert a string to camelcase in Google Spreadsheet formula

好久不见. 提交于 2019-12-10 13:11:37

问题


Trying to create a formula to turn a string of words separated by spaces into camelcase


回答1:


This should work:

=JOIN("",ArrayFormula(UPPER(LEFT(SPLIT(A3," ")))&LOWER(MID(SPLIT(A3," "),2,500))))

or to be more precise:

=JOIN("",ArrayFormula(UPPER(LEFT(SPLIT(A3," ")))& LOWER(REGEXEXTRACT(SPLIT(A3," "),".(.*)"))))




回答2:


Much smaller version:

=SUBSTITUTE(PROPER(TRIM(A1))," ","")

We just use PROPER to upper case and TRIM and SUBSTITUTE to remove spaces.

If we want lowerCamelCase,

By just REPLACEing the first character with lower case, We have:

=REPLACE(SUBSTITUTE(PROPER(TRIM(A1))," ",),1,1,LEFT(LOWER(TRIM(A1))))

Using REGEX:

=REGEXREPLACE(REGEXREPLACE(PROPER(A1),"\s*",),"^(\w)",LEFT(LOWER(TRIM(A1))))

=LOWER(LEFT(TRIM(A1)))&REGEXREPLACE(PROPER(TRIM(A1)),"(\w|\s)(\w*)","$2")



回答3:


To do this the following formula works (where A3 is the cell)

tl;dr:

=IF(IFERROR(FIND(" ",A3)), CONCAT(SUBSTITUTE(LEFT(LOWER(A3), FIND(" ", A3)), " ", ""), SUBSTITUTE(PROPER(SUBSTITUTE(A3, LEFT(A3, FIND(" ", A3)), "")), " ", "")), LOWER(A3))

Annotated:

=IF(                               // if a single word
    IFERROR(                       // test if NOT an error
        FIND(                      // looking for a space
            " ",
            A3
        )
    ),
    CONCAT(                        // concat the first word with the rest
        SUBSTITUTE(                // remove the space
            LEFT(                  // left of the find
                LOWER(             // lowercase the string
                    A3
                ),
                FIND(              // find the space in the string
                    " ",
                    A3
                )
            ),
            " ",
            ""
        ),
        SUBSTITUTE(                // remove spaces
            PROPER(                // convert string to capitals
                SUBSTITUTE(        // remove first word
                    A3,
                    LEFT(          // left of the find
                        A3,
                        FIND(      // find first space
                            " ",
                            A3
                        )
                    ),
                    ""
                )
            ),
            " ",
            ""
        )
    ),
    LOWER(                      // lowercase rest of the word
        A3
    )
)



回答4:


If the string of words you're trying to turn into camel case is contained in A1, the formula is very simple:

=MINUSCULE(REGEXREPLACE(A1, " ", "_"))


来源:https://stackoverflow.com/questions/43399328/how-to-convert-a-string-to-camelcase-in-google-spreadsheet-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!