Created
October 2, 2014 06:41
-
-
Save ian128K/5f8476a2ceb3af366b9d to your computer and use it in GitHub Desktop.
Function for capitalising the first letters in a sentence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function FirstLetterCapitalise(message) { | |
if((typeof message === "undefined") || (typeof message !== "string")) { | |
return "You have to pass in a string."; | |
} else { | |
var buffer, blength, newbuf, newstr, nblength, char, result; | |
buffer = message.split(" "); | |
blength = buffer.length; | |
result = ""; | |
for(var i = 0; i < blength; i++) { | |
char = buffer[i].charAt(0); | |
if((char.match(/^[A-Za-z]+$/) === null) || (char.match(/^[A-Z]+$/))) { | |
result += buffer[i]; | |
} else if(char.match(/^[a-z]+$/)) { | |
newstr = buffer[i]; | |
newbuf = newstr.split(""); | |
newbuf[0] = newbuf[0].toUpperCase(); | |
nblength = newbuf.length | |
newstr = ""; | |
for(var j = 0; j < nblength; j++) { | |
newstr += newbuf[j]; | |
} | |
result += newstr; | |
} | |
if(i !== (blength - 1)) { | |
result += " "; | |
} | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment