首页 > 解决方案 > 识别字符串中的特定字符(为什么我的代码不起作用?)

问题描述

所以我正在尝试在 JavaScript 中搜索字符串,我的代码如下所示:

var countChar = function(n) {

    let checkPhrase = n;
    for (let i = 0; i < (checkPhrase.length); i++) {
        if ((checkPhrase.indexOf("_") > -1) == true) {
            console.log(checkPhrase[i])
      }
  };
};

countChar("_.");

当我运行代码时,输​​出是“_”和“。” 为什么?我的 if 语句只能在 checkPhrase === 0 的索引时为真

标签: javascriptstringloops

解决方案


推荐阅读