How to convert a string to camelcase in Google Spr

2019-06-21 03:15发布

问题:

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(C8))," ","")

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

EDIT:

It seems OP specifically wanted 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))))


回答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
    )
)