Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Implement String.prototype.toLowerCase()


The toLowerCase method returns the value of the string converted to lowercase. toLowerCase does not affect the value of the string itself.

Conditions Let count chars inside function and ignore all bootstrap code:

String.prototype.toLowerCase = function(){
  // code that count here

Other languages are welcome but JavaScript implemantation is target of this question. Let's just focus on ASCII charachters .

Example input and output:

"sOmeText".toLowerCase(); => "sometext"

Example implementation:

String.prototype.toLowerCase = function(){
  return this.split('').map(function(c){
     var cc = c.charCodeAt(0);
     if (cc > 64 && cc < 91) {
        return String.fromCharCode(cc + 32);
     return c;
share|improve this question
Winning condition? I saw String.prototype.toLowerCase.apply(new Date()); on StackOverflow with a quick search; what is the input going to be? – beary605 Feb 15 '13 at 1:00
@beary605 I've updated the question. I hope it's clear now. – Mohsen Feb 15 '13 at 4:22
The example answer is buggy. It doesn't even handle the whole of Latin 1. – Peter Taylor Feb 15 '13 at 7:15
It might want to be specified that the answer only has to work on ASCII characters. Otherwise, things may get out of hand quickly. – Mr. Llama Feb 15 '13 at 17:21
ú, obviously. That's what String.prototype.toLowerCase() does. – Peter Taylor Feb 15 '13 at 22:14

10 Answers 10

up vote 5 down vote accepted

Javascript 70 68

Building from @grc's answer:

for(s=i='';c=this[i++];s+=(parseInt(c,36)||c).toString(36));return s

Convert character to integer then back (works since Number.toString(36) will always be lowercase)

Test case:





Update: -2 characters, thanks @copy

share|improve this answer
Save 2 characters: for(s=i="" – copy Feb 15 '13 at 21:22
@copy didn't realize ''++ becomes 0 interesting. – Shmiddty Feb 15 '13 at 21:24

Bash, 10

Doesn't interface with javascript very well, but it works for everyday purposes.

tr A-Z a-z

Also, it's shorter than golfscript. (Booyah!)

Example Input:

echo "Hello World, asDFghJkL0123" | tr A-Z a-z


hello world, asdfghjkl0123
share|improve this answer
Kindly show an example of input and output. – DavidC Feb 15 '13 at 22:30
shorter than golfscript. This is a first. Is this a first? – beary605 Feb 16 '13 at 0:49

J, 25

toLowerCase =: u:@(+32*91&>*64&<)@(3&u:)

Converting to numbers (3&u:) and back u: is quite uneasy.

share|improve this answer

Golfscript, 17

share|improve this answer

Javascript, 86

String.prototype.toLowerCase = function () {
    s="";for(i=0;c=this.charCodeAt(i++);s+=String.fromCharCode(c>64&c<91?c+32:c));return s
share|improve this answer

Python (45)

l=lambda x:''.join(chr(ord(i)+('@'<i<'[')*32)for i in x)


l=func(x) {
  return toString( iterate(i in x) { chr(ord(i) + IsUppercase(i) * 32) } )

('@'<i<'[')*32): IsUppercase. '@' - 64, 'A' - 65, 'Z' - 90, '[' - 91: If 64<ord(i)<91, then return True and multiply it by 32 (to get 32).

share|improve this answer

VBA - 77 82

Function l(s)
'Counting only the stuff between Function and End
For x=1 To Len(s):m=Mid(s,x,1):l=l & IIf(m Like"[A-Z]",Chr(Asc(m)+32),m):Next
End Function
share|improve this answer

Ruby: 32 characters


Sample run:
(In 35 characters it can be made a method of String class, to work just as the JavaScript sample.)

irb(main):001:0> class String
irb(main):002:1>   def toLowerCase  
irb(main):003:2>     self.gsub(/[A-Z]/){($&.ord+32).chr}
irb(main):004:2>   end
irb(main):005:1> end
=> nil
irb(main):006:0> "sOmeText".toLowerCase      
=> "sometext"
share|improve this answer

javascript 76 chars:

replace(/[A-Z]/g,function(b){return String.fromCharCode(b.charCodeAt()+32)})

Here's the full function:

String.prototype.toLowerCase = function () {
    return this.replace(/[A-Z]/g, function(b){
        return String.fromCharCode(b.charCodeAt()+32)
share|improve this answer

Julia, 49 chars

L(s)=ascii([s[i]+32(64<s[i]<91)for i=1:endof(s)])

for all chars s[i] if 64 < s[i] < 91 is true, add 32 to the ASCII character point (32*true=32). If it’s false, then (32*false=0), and nothing is changed.


julia> str

julia> L(str)

The default method in Julia is lowercase(str)

julia> lowercase(str)

the difference in timing and memory allocation:

julia> @time L(str)
elapsed time: 1.0263e-5 seconds (792 bytes allocated)

julia> @time lowercase(str)
elapsed time: 6.693e-6 seconds (224 bytes allocated)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.