admin管理员组文章数量:1401773
I have a string of text that I've split into an array on each ma. I then looped through the array and added each element to a string, one by one, but separated them using a line break.
var beg2 = document.twocities.begins.value;
var ans22 = "";
var words2 = beg2.split(",");
for(var i=0; i<words2.length; i++){
ans22 += words2[i] + "<br>";
}
document.getElementById("ans22").innerHTML = ans22;
Now I'm trying to capitalize the first letter of each line using this code but only the first letter of the entire string ends up getting capitalized as opposed to the first on each line.
var ans23 = "";
for (var i=0; i<words2.length; i++){
firstLetter = words[i].charAt(0);
firstLetterCap = words[i].charAt(0).toUpperCase();
words[i].replace(firstLetter,firstLetterCap);
ans23 += words2[i] + "<br>";
}
Any suggestions would be greatly appreciated.
I have a string of text that I've split into an array on each ma. I then looped through the array and added each element to a string, one by one, but separated them using a line break.
var beg2 = document.twocities.begins.value;
var ans22 = "";
var words2 = beg2.split(",");
for(var i=0; i<words2.length; i++){
ans22 += words2[i] + "<br>";
}
document.getElementById("ans22").innerHTML = ans22;
Now I'm trying to capitalize the first letter of each line using this code but only the first letter of the entire string ends up getting capitalized as opposed to the first on each line.
var ans23 = "";
for (var i=0; i<words2.length; i++){
firstLetter = words[i].charAt(0);
firstLetterCap = words[i].charAt(0).toUpperCase();
words[i].replace(firstLetter,firstLetterCap);
ans23 += words2[i] + "<br>";
}
Any suggestions would be greatly appreciated.
Share Improve this question edited Nov 17, 2018 at 17:37 Mohammad 21.5k16 gold badges57 silver badges85 bronze badges asked Nov 17, 2018 at 17:10 TehBrackShiipTehBrackShiip 551 silver badge5 bronze badges 1-
1
replace()
does not change a string in place, you have to save the return value from it – Patrick Evans Commented Nov 17, 2018 at 17:12
2 Answers
Reset to default 6You can simplify this considerably with map
by transforming each word in the sentence to its capitalized version and then joining the array back into a sentence:
var sentence = 'hello world test';
var capitalized = sentence
.split(' ')
.map(w => w.charAt(0).toUpperCase() + w.slice(1))
.join('<br>');
console.log(capitalized);
You don't need to use .replace()
. Use .charAt(0)
to selecting first character of string and use .slice()
to selecting next part of string
var beg2 = "first,second,third";
var ans22 = "";
var words = beg2.split(",");
for (var i=0; i<words.length; i++){
words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
ans22 += words[i] + "<br>";
}
document.getElementById("ans22").innerHTML = ans22;
<div id="ans22"></div>
Also you can use CSS ::first-letter
pseudo-element and text-transform
property to do this work.
var str = "first,second,third";
document.getElementById("ans22").innerHTML = str.split(",").map(function(val){
return "<div>"+val+"</div>"
}).join("");
#ans22 div::first-letter {
text-transform: capitalize;
}
<div id="ans22"></div>
本文标签: Capitalize first letter of each array element using JavaScriptStack Overflow
版权声明:本文标题:Capitalize first letter of each array element using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744322802a2600584.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论