Skip to content

Instantly share code, notes, and snippets.

@ian128K
Created October 2, 2014 06:41
Show Gist options
  • Save ian128K/5f8476a2ceb3af366b9d to your computer and use it in GitHub Desktop.
Save ian128K/5f8476a2ceb3af366b9d to your computer and use it in GitHub Desktop.
Function for capitalising the first letters in a sentence
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