How can I capitalize the first letter of each word?

前端 未结 20 2360
暖寄归人
暖寄归人 2021-01-14 12:31

I need a script in any language to capitalize the first letter of every word in a file.

Thanks for all the answers.

Stackoverflow rocks!

20条回答
  •  北荒
    北荒 (楼主)
    2021-01-14 12:46

    Here's another Ruby solution, using Ruby's nice little one-line scripting helpers (automatic reading of input files etc.)

    ruby -ni~ -e "puts $_.gsub(/\b\w+\b/) { |word| word.capitalize }" foo.txt
    

    (Assuming your text is stored in a file named foo.txt.)

    Best used with Ruby 1.9 and its awesome multi-language support if your text contains non-ASCII characters.

提交回复
热议问题