2
$\begingroup$

Is there some simpler way than the one below to convert a CamelCased string to a string consisting of separate words?

A crude way, with tedious typing, is the following:

unCamelCase[str_] := 
 StringDrop[
  StringJoin[
   StringSplit[
    str, {"A" -> " a", "B" -> " b", "C" -> " c", "H" -> " h", 
     "I" -> " i", "O" -> " o", "S" -> " s", "Z" -> " z"}]], 1]

(In the above I omitted from the list of replacements in StringSplit the letters I don't need for the particular example below, but for the method being used, in general I'd need the list of all 26 replacements.)

For example:

   unCamelCase["CompactSubspaceOfHausdorffSpaceIsClosed"]
(* compact subspace of hausdorff space is closed *)
$\endgroup$
9
$\begingroup$
StringReplace["CompactSubspaceOfHausdorffSpaceIsClosed", 
 u_?UpperCaseQ :> " " <> ToLowerCase[u]]

(*" compact subspace of hausdorff space is closed"*)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.