(编辑:jimmy 日期: 2024/11/10 浏览:2)
本文基于Free Code Camp基本算法脚本“标题案例一句”。
在此算法中,我们要更改文本字符串,以便每个单词的开头始终都有一个大写字母。
在本文中,我将解释三种方法。首先使用FOR循环,其次使用map()方法,第三次使用replace()方法。
对于此解决方案,我们将使用String.prototype.toLowerCase()方法
String.prototype.split()方法,String.prototype.charAt()方法
String.prototype.slice()方法和Array.prototype.join()方法
我们将需要在split()方法的括号之间添加一个空格
var strSplit = "I'm a little tea pot".split(' ');
它将输出一个由单词组成的数组:
var strSplit = ["I'm", "a", "little", "tea", "pot"];
如果不在括号中添加空格,则将得到以下输出:
var strSplit = ["I", "'", "m", " ", "a", " ", "l", "i", "t", "t", "l", "e", " ", "t", "e", "a", " ", "p", "o", "t"];
我们将其合并
str[i].charAt(0).toUpperCase()
在FOR循环中将大写前的字符串索引0字符
和
str[i].slice(1)
将从索引1提取到字符串的末尾。
为了标准化,我们将整个字符串设置为小写。
有注释:
function titleCase(str) { // Step 1. Lowercase the string str = str.toLowerCase(); // str = "I'm a little tea pot".toLowerCase(); // str = "i'm a little tea pot"; // Step 2. Split the string into an array of strings str = str.split(' '); // str = "i'm a little tea pot".split(' '); // str = ["i'm", "a", "little", "tea", "pot"]; // Step 3. Create the FOR loop for (var i = 0; i < str.length; i++) { str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1); /* Here str.length = 5 1st iteration: str[0] = str[0].charAt(0).toUpperCase() + str[0].slice(1); str[0] = "i'm".charAt(0).toUpperCase() + "i'm".slice(1); str[0] = "I" + "'m"; str[0] = "I'm"; 2nd iteration: str[1] = str[1].charAt(0).toUpperCase() + str[1].slice(1); str[1] = "a".charAt(0).toUpperCase() + "a".slice(1); str[1] = "A" + ""; str[1] = "A"; 3rd iteration: str[2] = str[2].charAt(0).toUpperCase() + str[2].slice(1); str[2] = "little".charAt(0).toUpperCase() + "little".slice(1); str[2] = "L" + "ittle"; str[2] = "Little"; 4th iteration: str[3] = str[3].charAt(0).toUpperCase() + str[3].slice(1); str[3] = "tea".charAt(0).toUpperCase() + "tea".slice(1); str[3] = "T" + "ea"; str[3] = "Tea"; 5th iteration: str[4] = str[4].charAt(0).toUpperCase() + str[4].slice(1); str[4] = "pot".charAt(0).toUpperCase() + "pot".slice(1); str[4] = "P" + "ot"; str[4] = "Pot"; End of the FOR Loop*/ } // Step 4. Return the output return str.join(' '); // ["I'm", "A", "Little", "Tea", "Pot"].join(' ') => "I'm A Little Tea Pot" } titleCase("I'm a little tea pot");
没有注释:
function titleCase(str) { str = str.toLowerCase().split(' '); for (var i = 0; i < str.length; i++) { str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1); } return str.join(' '); } titleCase("I'm a little tea pot");
对于此解决方案,我们将使用Array.prototype.map()方法。
如上例所示,在应用map()方法之前,我们将小写并分割字符串。
代替使用FOR循环,我们将把map()方法作为条件与上一个示例的连接相同。
(word.charAt(0).toUpperCase() + word.slice(1));
有注释:
function titleCase(str) { // Step 1. Lowercase the string str = str.toLowerCase() // str = "i'm a little tea pot"; // Step 2. Split the string into an array of strings .split(' ') // str = ["i'm", "a", "little", "tea", "pot"]; // Step 3. Map over the array .map(function(word) { return (word.charAt(0).toUpperCase() + word.slice(1)); /* Map process 1st word: "i'm" => (word.charAt(0).toUpperCase() + word.slice(1)); "i'm".charAt(0).toUpperCase() + "i'm".slice(1); "I" + "'m"; return "I'm"; 2nd word: "a" => (word.charAt(0).toUpperCase() + word.slice(1)); "a".charAt(0).toUpperCase() + "".slice(1); "A" + ""; return "A"; 3rd word: "little" => (word.charAt(0).toUpperCase() + word.slice(1)); "little".charAt(0).toUpperCase() + "little".slice(1); "L" + "ittle"; return "Little"; 4th word: "tea" => (word.charAt(0).toUpperCase() + word.slice(1)); "tea".charAt(0).toUpperCase() + "tea".slice(1); "T" + "ea"; return "Tea"; 5th word: "pot" => (word.charAt(0).toUpperCase() + word.slice(1)); "pot".charAt(0).toUpperCase() + "pot".slice(1); "P" + "ot"; return "Pot"; End of the map() method */ }); // Step 4. Return the output return str.join(' '); // ["I'm", "A", "Little", "Tea", "Pot"].join(' ') => "I'm A Little Tea Pot" } titleCase("I'm a little tea pot");
没有注释:
function titleCase(str) { return str.toLowerCase().split(' ').map(function(word) { return (word.charAt(0).toUpperCase() + word.slice(1)); }).join(' '); } titleCase("I'm a little tea pot");
对于此解决方案,我们将继续使用Array.prototype.map()方法并添加String.prototype.replace()方法。
replace()的方法返回与一些或通过替换替换的图案的所有比赛的新字符串。
在我们的例子中,replace()方法的模式将是一个字符串,该字符串将被新的替换替换,并将被视为逐字字符串。我们还可以使用正则表达式作为模式来解决此算法。
如将在第一个示例中看到的那样,在应用map()方法之前,我们将小写并拆分字符串。
有注释:
function titleCase(str) { // Step 1. Lowercase the string str = str.toLowerCase() // str = "i'm a little tea pot"; // Step 2. Split the string into an array of strings .split(' ') // str = ["i'm", "a", "little", "tea", "pot"]; // Step 3. Map over the array .map(function(word) { return word.replace(word[0], word[0].toUpperCase()); /* Map process 1st word: "i'm" => word.replace(word[0], word[0].toUpperCase()); "i'm".replace("i", "I"); return word => "I'm" 2nd word: "a" => word.replace(word[0], word[0].toUpperCase()); "a".replace("a", "A"); return word => "A" 3rd word: "little" => word.replace(word[0], word[0].toUpperCase()); "little".replace("l", "L"); return word => "Little" 4th word: "tea" => word.replace(word[0], word[0].toUpperCase()); "tea".replace("t", "T"); return word => "Tea" 5th word: "pot" => word.replace(word[0], word[0].toUpperCase()); "pot".replace("p", "P"); return word => "Pot" End of the map() method */ }); // Step 4. Return the output return str.join(' '); // ["I'm", "A", "Little", "Tea", "Pot"].join(' ') => "I'm A Little Tea Pot" } titleCase("I'm a little tea pot");
没有注释:
function titleCase(str) { return str.toLowerCase().split(' ').map(function(word) { return word.replace(word[0], word[0].toUpperCase()); }).join(' '); } titleCase("I'm a little tea pot");