convert strings to upper case code example

Example 1: string to uppercase

const upperCase = function(names){
    const toLower = names.toLowerCase().split(' ');
    const namesUpper = [];
    for(const wordName of toLower){
        namesUpper.push(wordName[0].toUpperCase() + wordName.slice(1));
    }
    return namesUpper.join(' ');
}

Example 2: best method to convert string to upper case manually

function myToUpperCase(str) {
  var newStr = '';
  for (var i=0;i<str.length;i++) {
    var thisCharCode = str[i].charCodeAt(0);
    if ((thisCharCode>=97 && thisCharCode<=122)||(thisCharCode>=224 && thisCharCode<=255)) {
    	newStr += String.fromCharCode(thisCharCode - 32);
    } else {
    	newStr += str[i];
    }
  }
  return newStr;
}
console.log(myToUpperCase('helLo woRld!')); // => HELLO WORLD!
console.log(myToUpperCase('üñïçødê')); // => ÜÑÏÇØDÊ

Tags:

Java Example