View Subcategory

Make a string uppercase

Transform "Space Monkey" into "SPACE MONKEY"
ruby
uppper = text.upcase
java
String upper = text.toUpperCase();
perl
print uc "Space Monkey"
groovy
println "Space Monkey".toUpperCase()
def lower = 'Space Monkey'
assert lower.toUpperCase() == 'SPACE MONKEY'
scala
println("Space Monkey".toUpperCase)
python
"Space Monkey".upper()
cpp
String(L"Space Monkey").ToUpper();
std::string s = "Space Monkey";
std::transform(s.begin(), s.end(), s.begin(), std::toupper);
std::string s = "Space Monkey";
boost::to_upper(s);
fsharp
printfn "%s" ("Space Monkey".ToUpper())
printfn "%s" (String.uppercase "Space Monkey")
erlang
io:format("~s~n", [string:to_upper("Space Monkey")]).
ocaml
String.uppercase "Space Monkey";;
csharp
string output = "Space Monkey"

System.Console.WriteLine(output.ToUpper())
php
echo strtoupper("Space Monkey");
haskell
toUpperCase oldstring converted = if oldstring == ""
then converted
else toUpperCase (tail(oldstring)) (converted ++ [Char.toUpper(head(oldstring))])

toUpperCase "Space Monkey" ""
toUpperCase = map Char.toUpper

toUpperCase "Space Monkey"
clojure
(.toUpperCase "Space Monkey")
fantom
s := "Space Monkey".localeUpper
go
strings.ToUpper("Space Monkey")

Make a string lowercase

Transform "Caps ARE overRated" into "caps are overrated"
ruby
"Caps ARE overRated".downcase
java
"Caps ARE overRated".toLowerCase();
perl
print lc "Caps ARE overRated"
groovy
println "Caps ARE overRated".toLowerCase()
scala
"Caps ARE overRated".toLowerCase
python
"Caps ARE overRated".lower()
cpp
std::string s = "Caps ARE overRated";
std::string sl(boost::to_lower_copy(s));
String(L"Caps ARE overRated").ToLower();
fsharp
printfn "%s" ("Caps ARE overRated".ToLower())
printfn "%s" (String.lowercase "Caps ARE overRated")
erlang
io:format("~s~n", [string:to_lower("Caps ARE overRated")]).
ocaml
String.lowercase "Caps ARE overRated";;
csharp
string str = "Caps ARE overRated";
str = str.ToLower() ;
Console.WriteLine(str);
php
echo strtolower("Caps ARE overRated");
haskell
import Char
str = map toLower "Caps ARE overRated"
clojure
(.toLowerCase "Caps ARE overRated")
fantom
s := "Caps ARE overRated".localeLower
go
strings.ToLower("Caps ARE overRated")

Capitalise the first letter of each word

Transform "man OF stEEL" into "Man Of Steel"
ruby
caps = text.gsub(/\w+/) { $&.capitalize }
caps = text.split.each{|i| i.capitalize!}.join(' ')
text.split.map(&:capitalize) * ' '
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();
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)));
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);
String text = WordUtils.capitalizeFully("man OF stEEL");
perl
$text =~ s/(\w+)/\u\L$1/g;
groovy
def capitalize(s) { s[0].toUpperCase() + s[1..-1].toLowerCase() }
caps = "man OF stEEL".replaceAll(/\w+/) { w -> capitalize(w) }
caps = "man OF stEEL".replaceAll(/\w+/) { w -> StringUtils.capitalize(w.toLowerCase()) }
caps = WordUtils.capitalizeFully("man OF stEEL")
"man OF stEEL".toLowerCase().tokenize().collect { it.capitalize() }.join(' ')
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(" ")}
val text = WordUtils.capitalizeFully("man OF stEEL")
val text = StringUtils.join("man OF stEEL".split("\\s") map {(x) => StringUtils.capitalize(x.toLowerCase) + " "})
// can be solved without external libraries
(("man OF stEEL" toLowerCase) split " " map (_ capitalize)).mkString(" ")
// 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 " "
(" " + "man OF stEEL").sliding(2).toList.map( x => x(0) match {
case ' ' => x(1).toUpper
case _ => x(1).toLower
}).mkString
python
from string import capwords
capwords("man OF stEEL")
' '.join(s.capitalize() for s in "man OF stEEL".split())
"man OF stEEL".title()
cpp
std::string words = "mAn OF stEEL";
std::transform(words.begin(), words.end(), words.begin(), ToCaps<>());
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]);
}
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();
fsharp
let words = String.Join(" ", Array.map (fun (s : String) -> (String.capitalize (s.ToLower()))) ("man OF stEEL".Split [|' '|]))
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))
// 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 " "
let culture = System.Globalization.CultureInfo.GetCultureInfo("en-US")
let titleCase = culture.TextInfo.ToTitleCase "man oF sTeel"
erlang
Caps = string:join(lists:map(fun(S) -> to_caps(S) end, string:tokens("man OF stEEL", " ")), " "),
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")
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");;
csharp
System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase("man OF stEEL".ToLowerInvariant());
php
echo ucwords(strtolower("man OF stEEL"));
haskell
import Data.Char

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