How to convert part of a string to uppercase start from specific index in JavaScript

1 Answer

0 votes
String.prototype.replaceAt=function(i, replacement_ch_s) {
    return this.substr(0, i) + replacement_ch_s + this.substr(i + replacement_ch_s.length);
}
 
function convert_part_to_uppercase(s, idx) { 
    var len = s.length;
    
    if (idx < 0 || idx > len) return s;
   
    for (var i = 0; i < len; i++) { 
         if (i >= idx && s[i] >= 'a' && s[i] <= 'z') {
             s = s.replaceAt(i, s.charAt(i).toUpperCase());
         }
    } 
    return s;
} 
       
   
var s = "javascript programming";
 
s = convert_part_to_uppercase(s, 4);
document.write(s + "<br />"); 


   
   
/*
run:
    
javaSCRIPT PROGRAMMING
    
*/

 



answered Nov 22, 2019 by avibootz
...