简体   繁体   中英

JavaScript: Amend the Sentence

I am having trouble below javaScript problem.

Question: You have been given a string s, which is supposed to be a sentence. However, someone forgot to put spaces between the different words, and for some reason they capitalized the first letter of every word. Return the sentence after making the following amendments:

Put a single space between the words. Convert the uppercase letters to lowercase.

Example

"CodefightsIsAwesome", the output should be "codefights is awesome";

"Hello", the output should be "hello".

My current code is:

Right now, my second for-loop just manually slices the parts from the string. How can I make this dynamic and insert "space" in front of the Capital String?

You can use String.prototype.match() with RegExp /[AZ][^AZ]*/g to match AZ followed by one or more characters which are not AZ , or character at end of string; chain Array.prototype.map() to call .toLowerCase() on matched words, .join() with parameter " " to include space character between matches at resulting string.

 var str = "CodefightsIsAwesome"; var res = str.match(/[AZ][^AZ]*/g).map(word => word.toLowerCase()).join(" "); console.log(res);

Alternatively, as suggested by @FissureKing, you can use String.prototype.repalce() with .trim() and .toLowerCase() chained

 var str = "CodefightsIsAwesome"; var res = str.replace(/[AZ][^AZ]*/g, word => word + ' ').trim().toLowerCase(); console.log(res);

Rather than coding a loop, I'd do it in one line with a (reasonably) simple string replacement:

 function amendTheSentence(s) { return s.replace(/[AZ]/g, function(m) { return " " + m.toLowerCase() }) .replace(/^ /, ""); } console.log(amendTheSentence("CodefightsIsAwesome")); console.log(amendTheSentence("noCapitalOnFirstWord")); console.log(amendTheSentence("ThereIsNobodyCrazierThanI"));

That is, match any uppercase letter with the regular expression /[AZ]/ , replace the matched letter with a space plus that letter in lowercase, then remove any space that was added at the start of the string.

Further reading:

We can loop through once.

The below assumes the very first character should always be capitalized in our return array. If that is not true, simply remove the first if block from below.

For each character after that, we check to see if it is capitalized. If so, we add it to our return array, prefaced with a space. If not, we add it as-is into our array.

Finally, we join the array back into a string and return it.

 const sentence = "CodefightsIsAwesome"; const amend = function(s) { ret = []; for (let i = 0; i < s.length; i++) { const char = s[i]; if (i === 0) { ret.push(char.toUpperCase()); } else if (char.toUpperCase() === char) { ret.push(` ${char.toLowerCase()}`); } else { ret.push(char); } } return ret.join(''); }; console.log(amend(sentence));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM