String list manipulation
String list manipulation
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?
words
Strings go between
"
quotes in Mathematica.– Szabolcs
Aug 21 at 7:31
"
2 Answers
2
If the input and the desired output are lists of String
s:
String
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
StringReplace[strings, WordBoundary ~~ a_ :> ToUpperCase[a]]
"Cut", "Was", "Saw", "Clear", "Sharp", "Keen", "Tree", "These"
If the input and desired output are lists of Symbol
s:
Symbol
words = cut, was, saw, clear, sharp, keen, tree, these;
Symbol /@ GeneralUtilities`ToTitleCase[ToString /@ words]
Cut, Was, Saw, Clear, Sharp, Keen, Tree, These
Could also use Capitalize
:
Capitalize
Capitalize["cut", "was", "saw", "clear", "sharp", "keen", "tree", "these"]
"Cut", "Was", "Saw", "Clear", "Sharp", "Keen", "Tree", "These"
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
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?– Kuba♦
Aug 21 at 5:24