Difference between revisions of "Case conversion"

From CodeCodex

(Erlang)
Line 1: Line 1:
==Assembly==
+
==Implementation==
 +
 
 +
===Assembly===
 
<pre>
 
<pre>
 
mov edx,offset string ; string source goes into edx somehow
 
mov edx,offset string ; string source goes into edx somehow
Line 15: Line 17:
 
</pre>
 
</pre>
  
==BASIC==
+
===BASIC===
 
<pre>
 
<pre>
 
a$ = "HELLO world"
 
a$ = "HELLO world"
Line 39: Line 41:
 
</pre>
 
</pre>
  
==C==
+
===C===
 
<pre class="c">
 
<pre class="c">
 
#include <ctype.h>
 
#include <ctype.h>
Line 78: Line 80:
 
</pre>
 
</pre>
  
==C++==
+
===C++===
 
<pre class="c++">
 
<pre class="c++">
 
#include <algorithm>
 
#include <algorithm>
Line 92: Line 94:
 
</pre>
 
</pre>
  
==ColdFusion==
+
===ColdFusion===
 
ColdFusion has the built-in Functions <code>Ucase(string)</code> and <code>Lcase(string)</code>.
 
ColdFusion has the built-in Functions <code>Ucase(string)</code> and <code>Lcase(string)</code>.
  
==Go==
+
===Erlang===
 +
Erlang strings module has the builtin Functions to_lower and to_upper
 +
<pre>
 +
1> string:to_lower("Hello, World!").
 +
"hello, world!"
 +
2> string:to_upper("Hello, World!").   
 +
"HELLO, WORLD!"
 +
</pre>
 +
 
 +
===Go===
 
Go's strings module has ToUpper and ToLower functions:
 
Go's strings module has ToUpper and ToLower functions:
<highlightsyntax>
+
<pre>
 
package main
 
package main
 
import (
 
import (
Line 106: Line 117:
 
   fmt.Println(strings.ToUpper("éloïse"))
 
   fmt.Println(strings.ToUpper("éloïse"))
 
}
 
}
</highlightsyntax>
+
</pre>
  
==Haskell==
+
===Haskell===
 
<code>Char.toLower</code> and <code>Char.toUpper</code> converts a character to lowercase and uppercase, respectively. Since Strings in Haskell are just lists of Chars, we can just use "map" to operate on a whole string.
 
<code>Char.toLower</code> and <code>Char.toUpper</code> converts a character to lowercase and uppercase, respectively. Since Strings in Haskell are just lists of Chars, we can just use "map" to operate on a whole string.
 
  Prelude> map Char.toUpper "HELLO world"
 
  Prelude> map Char.toUpper "HELLO world"
 
  "HELLO WORLD"
 
  "HELLO WORLD"
  
==Java==
+
===Java===
 
Java's built in String class has methods called <code>toUpperCase</code> and <code>toLowerCase</code>.
 
Java's built in String class has methods called <code>toUpperCase</code> and <code>toLowerCase</code>.
  
==OCaml==
+
===OCaml===
 
<code>String.lowercase</code> and <code>String.uppercase</code>. The standard library assumes a latin1 (iso 8859-1) encoding and will do the conversions of accented letters accordingly:
 
<code>String.lowercase</code> and <code>String.uppercase</code>. The standard library assumes a latin1 (iso 8859-1) encoding and will do the conversions of accented letters accordingly:
 
  # print_endline (String.uppercase "éloïse");;
 
  # print_endline (String.uppercase "éloïse");;
Line 122: Line 133:
 
  - : unit = ()
 
  - : unit = ()
  
==Perl==
+
===Perl===
 
Perl has builtin functions called <code>lc</code>, <code>lcfirst</code>, <code>uc</code>, and <code>ucfirst</code>. Unlike the majority of other languages, they properly follow locale conventions and employ Unicode case equivalent mapping.
 
Perl has builtin functions called <code>lc</code>, <code>lcfirst</code>, <code>uc</code>, and <code>ucfirst</code>. Unlike the majority of other languages, they properly follow locale conventions and employ Unicode case equivalent mapping.
<!--HIGHLIGHTSYNTAX language="perl"-->
+
<pre class="perl">
 
  use utf8; # specify encoding of source, we can use literal characters now
 
  use utf8; # specify encoding of source, we can use literal characters now
 
   
 
   
Line 138: Line 149:
 
  uc 'ı'; # I
 
  uc 'ı'; # I
 
  lc 'I'; # ı
 
  lc 'I'; # ı
<!--/HIGHLIGHTSYNTAX-->
+
</pre>
  
==PHP==
+
===PHP===
 
PHP has builtin functions called <code>strtolower</code>, <code>strtoupper</code>, <code>ucfirst</code> and <code>ucwords</code>
 
PHP has builtin functions called <code>strtolower</code>, <code>strtoupper</code>, <code>ucfirst</code> and <code>ucwords</code>
  
==Python==
+
===Python===
 
Python has the builtin String Methods <code>lower</code>, <code>swapcase</code>, <code>capitalize</code> , <code>title</code> and <code>upper</code>.
 
Python has the builtin String Methods <code>lower</code>, <code>swapcase</code>, <code>capitalize</code> , <code>title</code> and <code>upper</code>.
  
 
If using non-latin characters, use <code>unicode('STRiNG').lower()</code>, etc..
 
If using non-latin characters, use <code>unicode('STRiNG').lower()</code>, etc..
  
==Rexx==
+
===Rexx===
 
The Rexx <code>translate</code> function is quite powerful--it takes four arguments in general, a string to be translated, an output table, an input table, and a pad character.  When called with one argument, it returns the uppercase equivalent of its input.  Going to lowercase is a bit uglier.
 
The Rexx <code>translate</code> function is quite powerful--it takes four arguments in general, a string to be translated, an output table, an input table, and a pad character.  When called with one argument, it returns the uppercase equivalent of its input.  Going to lowercase is a bit uglier.
  
Line 160: Line 171:
 
</pre>
 
</pre>
  
==Ruby==
+
===Ruby===
 
Ruby has the builtin String Methods <code>downcase</code>, <code>upcase</code>, <code>swapcase</code> and <code>capitalize</code>. Each of these methods also has a in-place modifying version, with an exclamation mark at the end of the name (i.e. <code>downcase!</code>).
 
Ruby has the builtin String Methods <code>downcase</code>, <code>upcase</code>, <code>swapcase</code> and <code>capitalize</code>. Each of these methods also has a in-place modifying version, with an exclamation mark at the end of the name (i.e. <code>downcase!</code>).
  
Line 174: Line 185:
 
[[Category:C++]]
 
[[Category:C++]]
 
[[Category:ColdFusion]]
 
[[Category:ColdFusion]]
 +
[[Category:Erlang]]
 
[[Category:Go]]
 
[[Category:Go]]
 
[[Category:Haskell]]
 
[[Category:Haskell]]

Revision as of 15:28, 2 March 2011

Implementation

Assembly

mov edx,offset string ; string source goes into edx somehow
upr_loop:
mov al,BYTE PTR [edx]
cmp al,97
jl upr_pool
cmp al,122
jg upr_pool
sub BYTE PTR [edx],32
upr_pool:
add edx,1
or al,al
jnz upr_loop

BASIC

a$ = "HELLO world"
b$ = "HELLO world"

'Convert a string to lowercase
LET temp$ = ""
FOR x = 1 to LEN(a$)
    LET c = ASC(MID$(a$,x,1))
    IF c > 64 AND c < 91 THEN LET c = c + 32
    LET temp$ = temp$ + CHR$(c)
NEXT x
LET a$ = temp$

'Convert a string to uppercase
LET temp$ = ""
FOR x = 1 to LEN(b$)
    LET c = ASC(MID$(b$,x,1))
    IF c > 96 AND c < 123 THEN LET c = c - 32
    LET temp$ = temp$ + CHR$(c)
NEXT x
LET b$ = temp$

C

#include <ctype.h>

/*
 * Convert a string to lowercase
 */
char *lc(char *str)
{
   char *t;

   for (t = str; *t; t++)
   {
      /* Convert to lowercase */
      (*t) = tolower(*t);
   }

   /* Done */
   return (str);
}

/*
 * Convert a string to uppercase
 */
char *uc(char *str)
{
   char *t;

   for (t = str; *t; t++)
   {
      /* Convert to uppercase */
      (*t) = toupper(*t);
   }

   /* Done */
   return (str);
}

C++

#include <algorithm>
#include <string>

using namespace std;

int main() {
    string value("HELLO world");
    transform(value.begin(), value.end(), value.begin(), static_cast<int(*)(int)> (toupper));
    // Note: static_cast is necessary since there exist multiple overloads of "toupper"
}

ColdFusion

ColdFusion has the built-in Functions Ucase(string) and Lcase(string).

Erlang

Erlang strings module has the builtin Functions to_lower and to_upper

1> string:to_lower("Hello, World!").
"hello, world!"
2> string:to_upper("Hello, World!").     
"HELLO, WORLD!"

Go

Go's strings module has ToUpper and ToLower functions:

package main
import (
  "fmt"
  "strings"
)
func main() {
  fmt.Println(strings.ToUpper("éloïse"))
}

Haskell

Char.toLower and Char.toUpper converts a character to lowercase and uppercase, respectively. Since Strings in Haskell are just lists of Chars, we can just use "map" to operate on a whole string.

Prelude> map Char.toUpper "HELLO world"
"HELLO WORLD"

Java

Java's built in String class has methods called toUpperCase and toLowerCase.

OCaml

String.lowercase and String.uppercase. The standard library assumes a latin1 (iso 8859-1) encoding and will do the conversions of accented letters accordingly:

# print_endline (String.uppercase "éloïse");;
ÉLOÏSE
- : unit = ()

Perl

Perl has builtin functions called lc, lcfirst, uc, and ucfirst. Unlike the majority of other languages, they properly follow locale conventions and employ Unicode case equivalent mapping.

 use utf8; # specify encoding of source, we can use literal characters now
 
 uc 'i'; # I - nothing fancy
 uc 'ö'; # Ö - not in ASCII, but still nothing fancy
 uc 'ĕ'; # Ĕ - some languages start puking because ĕ is not in ISO Latin-1
 uc 'б'; # Б - non-latin script handled just fine
 uc 'ß'; # SS - Unicode case equivalent mapping
 
 # under Turkish locale
 uc 'i'; # İ
 lc 'İ'; # i
 uc 'ı'; # I
 lc 'I'; # ı

PHP

PHP has builtin functions called strtolower, strtoupper, ucfirst and ucwords

Python

Python has the builtin String Methods lower, swapcase, capitalize , title and upper.

If using non-latin characters, use unicode('STRiNG').lower(), etc..

Rexx

The Rexx translate function is quite powerful--it takes four arguments in general, a string to be translated, an output table, an input table, and a pad character. When called with one argument, it returns the uppercase equivalent of its input. Going to lowercase is a bit uglier.


/* Translate string to uppercase */
string = translate(string)

/* Translate to lowercase */
string = translate(string, 'abcdefghijklmnopqrstuvwxyz','ABCDEFGHIJKLMNOPQRSTUVWXYZ')

Ruby

Ruby has the builtin String Methods downcase, upcase, swapcase and capitalize. Each of these methods also has a in-place modifying version, with an exclamation mark at the end of the name (i.e. downcase!).

Tcl

string tolower "ABCDEF"
string toupper "abcdef"