Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142188

Make a letter at given index in a string to uppercase

$
0
0

I want to convert a letter in a particular index in a string to uppercase.

I have a string:

let str = "Dwightschrute";

I could do:

let str = "Dwightschrute";

let a = str.slice(0, 6);
console.log(a); //Dwight

let b = (str.slice(6, 7)).toUpperCase();
console.log(b); // S

let c = str.slice(7);

console.log(c);

console.log(a+b+c); //DwightSchrute

Or

let str = "Dwightschrute";

str = [...str];

str[6] = str[6].toUpperCase();

console.log(str.join('')); //DwightSchrute

Is there a better way to do this? I think I am doing a lot of unnecessary stuff to achieve this


Viewing all articles
Browse latest Browse all 142188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>