2
$\begingroup$

I have a string list including some words. For example, I have

words={cut, was, saw, clear, sharp, keen, tree, these};

I want to write a code that changes the first letter of each word to the capital letter. I mean I want to have the result as

{Cut, Was, Saw, Clear, Sharp, Keen, Tree, These};

How can I figure it out?

$\endgroup$
  • 1
    $\begingroup$ It is not clear what do you mean by 'string list', there are no strings in words. Also, have you tried searching documentation for string related functions that could help you? $\endgroup$ – Kuba Aug 21 '18 at 5:24
  • $\begingroup$ Strings go between " quotes in Mathematica. $\endgroup$ – Szabolcs Aug 21 '18 at 7:31
3
$\begingroup$

If the input and the desired output are lists of Strings:

strings = {"cut", "was", "saw", "clear", "sharp", "keen", "tree", "these"};

GeneralUtilities`ToTitleCase[strings] 

{"Cut", "Was", "Saw", "Clear", "Sharp", "Keen", "Tree", "These"}

Alternatively, you can use StringReplace:

StringReplace[strings, WordBoundary ~~ a_ :> ToUpperCase[a]]

{"Cut", "Was", "Saw", "Clear", "Sharp", "Keen", "Tree", "These"}

If the input and desired output are lists of Symbols:

words = {cut, was, saw, clear, sharp, keen, tree, these};

Symbol /@ GeneralUtilities`ToTitleCase[ToString /@ words]

{Cut, Was, Saw, Clear, Sharp, Keen, Tree, These}

$\endgroup$
7
$\begingroup$

Could also use Capitalize:

Capitalize[{"cut", "was", "saw", "clear", "sharp", "keen", "tree", "these"}]

{"Cut", "Was", "Saw", "Clear", "Sharp", "Keen", "Tree", "These"}

$\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.