首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将第一个单词的第一个字母和单词的首字母大写为无条件大写的数组

将第一个单词的第一个字母和单词的首字母大写为无条件大写的数组
EN

Stack Overflow用户
提问于 2018-02-14 05:49:59
回答 3查看 89关注 0票数 1

这是我尝试过的代码

代码语言:javascript
复制
function sentenceCase(str, unconditionallyCapitalized) {
  let array = str
  str = str.toLowerCase().split(' ')
  console.log(str)
  // str.toUpperCase()
  unconditionallyCapitalized = unconditionallyCapitalized.join().toLowerCase()
  console.log(unconditionallyCapitalized)
  for (var x = 1; x <= array.length; x++) {
    str[0] = str[0].toUpperCase()
    if (str[x].includes('.')) {
      console.log(str[x])

    }
    if (unconditionallyCapitalized.includes(str[x])) {
      str[x] = str[x].split('')
      str[x][0] = str[x][0].toUpperCase()
      str[x] = str[x].join('')
      //console.log(str[x])
    }
  }

  return str.join(' ')
}


let str = 'I watched the storm, so beautiful yet terrific. The face of the moon was in shadow.';
let unconditionallyCapitalized = ['I', 'Moon', 'Shadow'];
console.log(sentenceCase(str, unconditionallyCapitalized));

所以上面的代码给我的是月亮,而不是阴影,而且“the”后面的“the”应该大写。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-02-14 06:20:17

尝试下一个代码:

代码语言:javascript
复制
  function findAndCapitalize(str, arrStr) {
        let tempStr = upperFirstLetter(str);
        arrStr.forEach(item => {
            let index = str.indexOf(item.toLowerCase());
            if(index !== -1) {
                let startPart = tempStr.substring(0, index),
                    endPart = tempStr.substring(index + item.length);
                tempStr = startPart + upperFirstLetter(item) + endPart;
            }
        })
        return tempStr;

        function upperFirstLetter(str) {
            let res = str.charAt(0).toUpperCase() + str.substring(1);
            return res;
        }
    }
    let str = 'I watched the storm, so beautiful yet terrific. The face of the moon was in shadow.';
    let unconditionallyCapitalized = ['I', 'Moon', 'Shadow'];
console.log(findAndCapitalize(str, unconditionallyCapitalized));

票数 1
EN

Stack Overflow用户

发布于 2018-02-14 06:06:28

这是一个更有帮助的答案:unconditionallyCapitalized.join().toLowerCase()得到了一个字符串i,moon,shadow,它没有'i', 'moon', 'shadow'数组那么有用

当一个单词有句点时,你只需要对它进行console.log。相反,您可以将it后面的单词大写为str[x+1] (这将是新句子中的第一个单词)。

我用string slicing替换了你的split/toUpperCase/join序列。

最后,你不需要去x <=array.length,只需要去x<array.length。额外的索引导致错误,因为它在该索引处找不到任何内容。

代码语言:javascript
复制
function sentenceCase(str, unconditionallyCapitalized) {

        str = str.toLowerCase().split(' ')
        unconditionallyCapitalized = unconditionallyCapitalized.map(item=>{return item.toLowerCase()})
        str[0] = str[0].toUpperCase()
        for (var x = 1; x < str.length; x++) {
            if (str[x].includes('.') && str[x+1]) {
                str[x+1] = str[x+1].slice(0, 1).toUpperCase() + str[x+1].slice(1)
            } else if (str[x].includes('.')) {
                str[x] = str[x].slice(0, -1)
            }
            if (unconditionallyCapitalized.includes(str[x])) {
                str[x] = str[x].slice(0, 1).toUpperCase() + str[x].slice(1)
            }
        }

        return str.join(' ') + '.'
    }


    var str = 'I watched the storm, so beautiful yet terrific. the face of the moon was in shadow.';
    var unconditionallyCapitalized = ['I', 'Moon', 'Shadow'];


var answer =  sentenceCase(str, unconditionallyCapitalized)
console.log(answer)

票数 0
EN

Stack Overflow用户

发布于 2018-02-14 06:14:28

这是一种现代的方法。这里的关键是在数组上使用.find()来检查您想要大写的单词列表(数组)中的单词是否与您正在迭代的当前单词匹配(通过空格将字符串分解为一个数组之后)。

.replace(/\W/g, '')会删除单词中的所有非单词字符,这使得shadow.可以匹配Shadow (当然是在小写之后)。

代码语言:javascript
复制
const cleanString = str => str.replace(/\W/g, '').toLowerCase();
const capitalizeCertainWords = (str, words) =>
    str
        .split(' ')
        .map(
            word =>
                words.find(
                    capitalizedWord =>
                        cleanString(word) === cleanString(capitalizedWord)
                )
                    ? word.substring(0, 1).toUpperCase() + word.substring(1)
                    : word
        )
        .join(' ');

const str =
    'I watched the storm, so beautiful yet terrific. The face of the moon was in shadow.';
const unconditionallyCapitalized = ['I', 'Moon', 'Shadow'];

console.log(
    capitalizeCertainWords(str, unconditionallyCapitalized)
);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48776220

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档