View Problem

Capitalise the first letter of each word

Transform "man OF stEEL" into "Man Of Steel"
ExpandDiskEdit
ruby
caps = text.gsub(/\w+/) { $&.capitalize }
ExpandDiskEdit
ruby
caps = text.split.each{|i| i.capitalize!}.join(' ')
DiskEdit
ruby
text.split.map(&:capitalize) * ' '
ExpandDiskEdit
java
String input = "man OF stEEL";
StringTokenizer tokenizer = new StringTokenizer(input);
StringBuffer sb = new StringBuffer();
while (tokenizer.hasMoreTokens()) {
String word = tokenizer.nextToken();
sb.append(word.substring(0, 1).toUpperCase());
sb.append(word.substring(1).toLowerCase());
sb.append(' ');
}
String text = sb.toString();
ExpandDiskEdit
java
StringBuilder sb = new StringBuilder("man OF stEEL"); String s = sb.toString();
int last = s.length() - 1;

for (int i = 0; i <= last; ++i)
if (Character.isSpaceChar(s.charAt(i)) && i < last) { ++i; sb.setCharAt(i, Character.toUpperCase(s.charAt(i))); }
else if (i == 0) sb.setCharAt(i, Character.toUpperCase(s.charAt(i)));
else sb.setCharAt(i, Character.toLowerCase(s.charAt(i)));
ExpandDiskEdit
java 1.4 or later
Matcher m = Pattern.compile("(\\w+)").matcher("man OF stEEL"); StringBuffer sb = new StringBuffer(32), rsb = new StringBuffer(8);

while (m.find())
{
rsb.replace(0, rsb.length(), m.group().toLowerCase()); rsb.setCharAt(0, Character.toUpperCase(rsb.charAt(0)));
m.appendReplacement(sb, rsb.toString());
}
m.appendTail(sb);
ExpandDiskEdit
java org.apache.commons
String text = WordUtils.capitalizeFully("man OF stEEL");
ExpandDiskEdit
perl
$text =~ s/(\w+)/\u\L$1/g;
ExpandDiskEdit
groovy
def capitalize(s) { s[0].toUpperCase() + s[1..-1].toLowerCase() }
caps = "man OF stEEL".replaceAll(/\w+/) { w -> capitalize(w) }
ExpandDiskEdit
groovy apache commons lang
caps = "man OF stEEL".replaceAll(/\w+/) { w -> StringUtils.capitalize(w.toLowerCase()) }
ExpandDiskEdit
groovy apache commons lang
caps = WordUtils.capitalizeFully("man OF stEEL")
DiskEdit
groovy
"man OF stEEL".toLowerCase().tokenize().collect { it.capitalize() }.join(' ')
ExpandDiskEdit
scala
def capitalize(s: String) = { s(0).toUpperCase + s.substring(1, s.length).toLowerCase }

"man OF stEEL".split("\\s") foreach {(x) => text.append(capitalize(x)).append(" ")}
ExpandDiskEdit
scala org.apache.commons
val text = WordUtils.capitalizeFully("man OF stEEL")
ExpandDiskEdit
scala org.apache.commons
val text = StringUtils.join("man OF stEEL".split("\\s") map {(x) => StringUtils.capitalize(x.toLowerCase) + " "})
ExpandDiskEdit
scala
// can be solved without external libraries
(("man OF stEEL" toLowerCase) split " " map (_ capitalize)).mkString(" ")
ExpandDiskEdit
scala
// This is just a slightly more compact form of the previous solution (my fav).
// It would be nice if split defaulted to whitespace (precompiled reg ex).
"man OF stEEL".toLowerCase.split(" ").map(_.capitalize) mkString " "
ExpandDiskEdit
scala
(" " + "man OF stEEL").sliding(2).toList.map( x => x(0) match {
case ' ' => x(1).toUpper
case _ => x(1).toLower
}).mkString
DiskEdit
python
from string import capwords
capwords("man OF stEEL")
DiskEdit
python 2.4
' '.join(s.capitalize() for s in "man OF stEEL".split())
DiskEdit
python
"man OF stEEL".title()
ExpandDiskEdit
cpp
std::string words = "mAn OF stEEL";
std::transform(words.begin(), words.end(), words.begin(), ToCaps<>());
ExpandDiskEdit
cpp C++/CLI .NET 2.0
StringBuilder^ sb = gcnew StringBuilder(L"man OF stEEL");

for (int i = 0, isFirst = 1; i < sb->Length; ++i)
{
sb[i] = Char::IsWhiteSpace(sb[i]) ? (isFirst = 1, sb[i]) : isFirst ? (isFirst = 0, Char::ToUpper(sb[i])) : Char::ToLower(sb[i]);
}
ExpandDiskEdit
cpp
std::string words = "mAn OF stEEL";
std::vector<std::string> swv;

boost::split(swv, words, boost::is_any_of(" "));
std::string newwords = (std::for_each(swv.begin(), swv.end(), StringTAndJ(WordToCaps))).value();
ExpandDiskEdit
fsharp
let words = String.Join(" ", Array.map (fun (s : String) -> (String.capitalize (s.ToLower()))) ("man OF stEEL".Split [|' '|]))
ExpandDiskEdit
fsharp
let wordlst = List.map (fun s -> (String.capitalize (String.lowercase s))) (String.split [' '] "man OF stEEL")
let words = new StringBuilder(List.hd wordlst)
for (s : String) in (List.tl wordlst) do (words.Append(" ").Append(s))
DiskEdit
fsharp 2.0
// Previous solutions used old library functions, here's something that works with F# 2.0
let s= "man OF stEEL"
let UpperFirst = function | "" -> "" | s -> s.Substring(0,1).ToUpper() + s.Substring(1).ToLower()
s.Split(' ') |> Array.map UpperFirst |> String.concat " "
ExpandDiskEdit
fsharp
let culture = System.Globalization.CultureInfo.GetCultureInfo("en-US")
let titleCase = culture.TextInfo.ToTitleCase "man oF sTeel"
ExpandDiskEdit
erlang
Caps = string:join(lists:map(fun(S) -> to_caps(S) end, string:tokens("man OF stEEL", " ")), " "),
DiskEdit
ocaml
let capitalize_words str =
let len = String.length str in
let res = String.copy str in
let rec aux i do_up =
if i >= len then res else
match str.[i] with
| ' ' | '\n' | '\t' | '\r' -> aux (succ i) true
| _ ->
res.[i] <-
(if do_up then Char.uppercase else Char.lowercase) str.[i];
aux (succ i) false
in
aux 0 true

let () =
print_endline (capitalize_words "man OF stEEL")
DiskEdit
ocaml
let capitalize_words str =
let capitalize_word w =
String.capitalize (String.lowercase w) in
let l = Str.split (Str.regexp " ") str in
String.concat " " (List.map (capitalize_word) l)
;;


let () =
print_endline (capitalize_words "man OF stEEL");;
DiskEdit
csharp
System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase("man OF stEEL".ToLowerInvariant());
ExpandDiskEdit
php
echo ucwords(strtolower("man OF stEEL"));
DiskEdit
haskell
import Data.Char

capitalizeWords = unwords . map capitalizeWord . words
where capitalizeWord [] = []
capitalizeWord (c:cs) = toUpper c : map toLower cs
DiskEdit
clojure
(use 'clojure.contrib.str-utils2)
(join " " (map capitalize (split "man OF stEEL" #" ")))
ExpandDiskEdit
fantom
"man OF stEEL".split.map { it.localeLower.localeCapitalize }.join(" ")
ExpandDiskEdit
go
strings.Title(strings.ToLower("man OF stEEL"))

Submit a new solution for ruby, java, perl, groovy ...