Convert a string to proper case
From CodeCodex
This code provides a method that turns improper casing (LIKE THIS or like this) to title casing (Like This).
Contents |
Implementations
Erlang
capitalize(S) ->
F = fun([H|T]) -> [string:to_upper(H) | string:to_lower(T)] end,
string:join(lists:map(F, string:tokens(S, " ")), " ").
Haskell
import Data.Char (toUpper, toLower) proper :: String -> String proper = unwords . map capitalize . words where capitalize (x:xs) = toUpper x : map toLower xs
For example:
> proper "foO BAr" "Foo Bar"
Java
Method
public String makeProper(String theString) throws java.io.IOException{
java.io.StringReader in = new java.io.StringReader(theString.toLowerCase());
boolean precededBySpace = true;
StringBuffer properCase = new StringBuffer();
while(true) {
int i = in.read();
if (i == -1) break;
char c = (char)i;
if (c == ' ' || c == '"' || c == '(' || c == '.' || c == '/' || c == '\\' || c == ',') {
properCase.append(c);
precededBySpace = true;
} else {
if (precededBySpace) {
properCase.append(Character.toUpperCase(c));
} else {
properCase.append(c);
}
precededBySpace = false;
}
}
return properCase.toString();
}
Test class
public static void main(String[] args) {
StringUtils util = new StringUtils();
try {
System.out.println(util.makeProper("testing tESTig TeStInG123 "+
"1jf94osl TEST TEST test test. doe, john. DOE, JOHN"));
} catch (java.io.IOException e) {
System.out.println("Exception:" + e);
}
}
Original Source: Java Forum
Method 2(Java 6 + RegEx)
public static String toProperCase(String input) {
//A pattern for all (UNICODE-) lower case characters preceded by a word boundary
Pattern p = Pattern.compile("\\b([\\p{javaLowerCase}])",Pattern.UNICODE_CASE);
Matcher m = p.matcher(input);
StringBuffer sb = new StringBuffer(input.length());
while (m.find()) {
m.appendReplacement(sb, m.group(1).toUpperCase());
}
m.appendTail(sb);
return sb.toString();
}
JUnit Test for Method 2
public void testProperCase() {
String test = StringUtil.toProperCase("es regnet-so.scho"n^im_walde");
assertEquals("Es Regnet-So.Scho"n^Im_walde", test);
}
Objective-C
NSString *a = @"foO BAr"; NSString *b = [a capitalizedString];
//This may be regarded more of a Cocoa solution than a strict Objective-C solution.
OCaml
# #load "str.cma";;
# let proper string =
let words = Str.split (Str.regexp " ") (String.lowercase string) in
String.concat " " (List.map String.capitalize words);;
val proper : string -> string = <fun>
For example:
# proper "hello world";; - : string = "Hello World"
Perl
print ucfirst lc for split /\b/;
PHP
$a = 'foO BAr'; $b = ucwords(strtolower($a));
Python
a = 'hello world! how are you?'
b = ' '.join(i.capitalize() for i in a.split(' '))
Alternatively:
import string b = string.capwords(a, ' ')
Another method:
b = a.title()
These two methods differ on how they determine word boundary. For example, title() considers apostrophes to separate words, while capwords() does not:
>>> string.capwords("they're bill's friends from the UK")
"They're Bill's Friends From The Uk"
>>> "they're bill's friends from the UK".title()
"They'Re Bill'S Friends From The Uk"
Ruby
"foO BAr".upcase # => "FOO BAR" "foO BAr".downcase # => "foo bar"
Tcl
proc to_proper_case s {return [string toupper [string index $s 0]][string tolower [string range $s 1 end]]}
test:
puts [to_proper_case "FoO BAr"] ;# -> Foo bar
also, builtin command "string totitle"
puts [string totitle "FoO BAr"] ;# -> Foo bar