This question already has answers here:
Convert string to Title Case with JavaScript
(68 answers)
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
How can I capitalize the first letter of each word in a string using JavaScript?
(46 answers)
Closed 4 years ago.
I basically want to capitalize the first letter in every word in a sentence, assuming that str is all lowercase. So here, I tried to split the string, letter by letter, then by using for loop, I would capitalize whatever the letter that's after a space. Here's my code and could you please point out where I coded wrong? Thank you.
function titleCase(str) {
var strArray = str.split('');
strArray[0].toUpperCase();
for (i=0; i<strArray.length;i++){
if (strArray[i]===" "){
strArray[i+1].toUpperCase();
}
}
return strArray.join('');
}
You need to assign the values:
function titleCase(str) {
var strArray = str.split('');
strArray[0] = strArray[0].toUpperCase();
for (i=0; i<strArray.length;i++){
if (strArray[i]===" "){
strArray[i+1] = strArray[i+1].toUpperCase();
}
}
return strArray.join('');
}
You can try following
function titleCase(str) {
var strArray = str.split(' ');
for (i=0; i<strArray.length;i++){
strArray[i] = strArray[i].charAt(0).toUpperCase() + strArray[i].slice(1);
}
return strArray.join(' ');
}
console.log(titleCase("i am a sentence"));
Related
This question already has answers here:
How to check if character is a letter in Javascript?
(17 answers)
Closed 3 years ago.
Hello I am having trouble trying to use Regex to check if each character in string is an alphabet.
First let me introduce the problem itself.
There is a string mixed with special chars and alphabets and suppose to return the number of alphabets only.
My code/pseudo code for problem is :
//Create var to hold count;
var count = 0;
//Loop thru str
for(let char of str){
//Check if char is a alphabet
***if(char === /[A-Za-z]/gi){***
//if so add to count
count ++;
}
//return count;
return count;
}
How can I use Regex in a conditional statement to check if each char is an alphabet????
Please help!
const pattern = /[a-z]/i
const result = [...'Abc1'].reduce((count,c) => pattern.test(c) ? count+1 : count, 0)
console.log(result) // 3
This question already has answers here:
Find words from array in string, whole words only (with hebrew characters)
(2 answers)
Match any non-word character (excluding diacritics)
(1 answer)
How to ban words with diacritics using a blacklist array and regex?
(5 answers)
Closed 3 years ago.
What is the correct way to check for a whole word in a unicode string in Javascript.
This works for ASCII only:
var strHasWord = function(word, str){
return str.match(new RegExp("\\b" + word + "\\b")) != null;
};
I tried XRegExp as follows but this does not work either.
var strHasWord = function(word, str){
// look for separators/punctuation characters or if the word is the first or the last in the string
var re = XRegExp("(\\p{Z}|\\p{P}|^)" + word + "(\\p{Z}|\\p{P}|$)");
return re.test(str);
//return str.match(re);
}
Any suggestions. Thanks.
EDIT 1
The following seems to do the trick.
var function strHasWord = function(word, str){
var re = RegExp("(\\p{Z}|\\p{P}|^)" + word + "(\\p{Z}|\\p{P}|$)", "u");
return str.match(re) != null;
//return re.test(str);
}
This question already has answers here:
How to capitalize first letter of each word, like a 2-word city? [duplicate]
(4 answers)
Closed 5 years ago.
i have a task for my homework where i have to write a function that will capitalize each word in a sentence that is written into that function. The idea i had was to convert each word into an array, make a loop targeting first letter of each item of that array, and then turning that array back into a string. The code i came up with is this
function titleCase(string) {
var words = string.split(' ');
for (var i = 0; i < words.length; i++) {
const lettersUp = ((words[i])[0]).toUpperCase();
const result = words[i].replace((words[i])[0], lettersUp);
return result;
}
}
The problem i have now is that it returns only the first word of an array. From troubleshooting i have been doing i have a feeling i messed up the loop but i just have no idea how. Any help would be greatly appreciated.
Thanks.
You are returning from the first iteration, so your code won't work.
What you are looking for is something like this:
function titleCase(string) {
var words = string.split(" ");
for (var i = 0; i < words.length; i++) {
const lettersUp = ((words[i])[0]).toUpperCase();
words[i] = words[i].replace((words[i])[0], lettersUp);
}
return words.join(" ");
}
Regexes are the way to go, though. Please try and use though.
Keep it as simple as possible - there's no need for additional variables such as lettersUp, you can simply manipulate the strings in the words array.
function titleCase(str) {
var words = str.split(' ');
for (var i = 0; i < words.length; i++) {
words[i] = words[i].charAt(0).toUpperCase() + words[i].substring(1);
}
return words.join(' ');
}
This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 7 years ago.
function to capitalise first letter of a string - 'toUpperCase' , underscore and other jQuery are excluded . I reworked a vers with underscore which I can't use
```
function capitalize (str){
var str = "";
var lowercase = "";
var Uppercase = "";
str.forEach(){
for (i=0; i < str.length; i++);
}
return Uppercase[lowercase.indexOf(str0)];
}
```
There are lots of reduced vers using toUpperCase
Any links, code help pls .... Tks
The best method I've found is just to call toUpperCase on the first character and concat the rest of the string using slice:
function capitalize(str) {
if(typeof str === 'string') {
return str[0].toUpperCase() + str.slice(1);
}
return str;
}
If you want to capitalize each word in a sentence, you can split on space:
"capitalize each word of this sentence".split(' ').map(capitalize).join(' ');
This question already has answers here:
Capitalize words in string [duplicate]
(21 answers)
Closed 7 years ago.
What to do - Capitalize the first letter of the words in a sentence.
So, I solved it and was wondering is there any way to do it without making it an array with .split().
What I tried without turning it into a array -
The logic - First, turn everything into lowercase. Then scan the sentence with a for loop, if you find a space, capitalize the next character.
function titleCase(str) {
str = str.toLowerCase();
for(i=0;i<str.length;i++) {
if(str[i]===" ") {
str = str.charAt[i+1].toUpperCase();
return str;
}
}
}
titleCase("I'm a little tea pot", "");
That code doesn't even run.
I just used split() and replace to do that. You can have a look at my code.
function titleCase (str)
{
str = str.split(' ');
for(var i=0;i<str.length;i++)
{
str[i] = str[i].replace(str[i][0],str[i][0].toUpperCase())
}
return str.join(' ');
}
var mainString ="i am strong!";
titleCase(mainString);
Here is one using replace + with a regex:
/**
* #summary Uppercase the first letter in a string.
* #returns {string}
*/
function uppercaseFirstLetters(string) {
return string.replace(/[a-zA-Z]*/g, function(match) {
return match.charAt(0).toUpperCase() + match.substr(1).toLowerCase();
})
}