如何在分割字符串后让charAt()检查每个第一个字母?

时间:2017-05-14 00:25:38

标签: javascript

如何让charAt()显示index = 0处所有分割值的字母?我不知道在.charAt()之前要写什么,以便安排在index = 0处给我所有分割值的字母,但它给了我一个值:

<button onclick="occurence('The quick brown fox jumps over the lazy 
dog')">Go</button>

<p id="demo"></p>

<script>
function occurence(str) {
//conversion to string to make get the value of every single letter
var spl = str.split("");
//loop to subtract from spl.length to get value for chartAt
for (i = 0; i < spl.length; i++) {
}
document.getElementById("demo").innerHTML = spl[i - spl.length].charAt(0) + 
"<br>";
}
</script>

5 个答案:

答案 0 :(得分:0)

这是您的代码的工作版本:

&#13;
&#13;
function occurence(str) {
  //conversion to string to make get the value of every single letter
  var spl = str.split(" ");
  //loop to subtract from spl.length to get value for chartAt
  var firstLetters = []
  for (i = 0; i < spl.length; i++) {
    firstLetters.push(spl[i].charAt(0));
  }
  document.getElementById("demo").innerHTML = firstLetters.join("<br>");
}
&#13;
<button onclick="occurence('The quick brown fox jumps over the lazy dog')">Go</button>

<p id="demo"></p>
&#13;
&#13;
&#13;

首先,您问题的The quick brown fox jumps over the lazy dog文字给了我一些错误。可能存在编码问题,因此我手动重写。

您应该将字符串拆分为str.split(" ")而不是str.split("")

您需要定义一个类似于此var firstLetters = []的数组,以将其用作for循环中的堆栈。

你得到spl[i].charAt(0)一个单词的第一个字母,你需要在for循环中用firstLetters.push(spl[i].charAt(0))推送这个字母。

最后,您加入firstLetters堆栈并在演示代码中显示:document.getElementById("demo").innerHTML = firstLetters.join("") + "<br>"

答案 1 :(得分:0)

我为没有正确解释我的问题而道歉。我试图创建一个程序,在两个文本框中输入一个用户输入文本,在该文本中,他们想知道在第一个文本框中输入的文本中找到了在第二个文本框中输入的指定字符的数量。你们很多人告诉我要分开“”而不是“”但是“”不会给我任何单一角色的字符(0)。我已经构建了程序并使用mertyildiran的代码来获取每个字母的charAt(0)。

    <!DOCTYPE html>
    <html>
   <head>
   <title>Page Title</title>
   </head>
   <body>

   enter text:
   <input id="enteredText" type="text">

   enter text to be checked:
   <input id="toBeChecked" type="text">

   <button onclick="occurence()">Go</button>

   <p id="demo"></p>

   <script>
   function occurence() {
   //value of entered text to be checked
   var inpTextToCheck = document.getElementById("toBeChecked").value
   //value of entered text
   var inpText = document.getElementById("enteredText").value

   //conversion to string to make get the value of every single letter
   var spl = inpText.split("");


   //loop to get all letters at index = 0(all letters actually, since they are 
   now an array)
   var allLetters = []
   for (i = 0; i < spl.length; i++) {
    allLetters.push(spl[i].charAt(0));
   }
   var inpLetters = allLetters.join();

   //conversion of letter or word entered by user to RegExp so it can be used as 
   a variable in match()
   var re = new RegExp(inpTextToCheck, 'gi');

   //number of times the text to be checked is found in the entered text
   numOfOccurences = (inpText.match(re) || []).length

   // if the text to be checked is a number and is found once in the entered 
   text
   if (numOfOccurences == 1 && inpTextToCheck >= 0 || inpTextToCheck <= 0) {
   document.getElementById("demo").innerHTML = "The number " + inpTextToCheck
   + " was found once in the entered text"
   }
   //if the text to be checked is a number and is found more than once in the 
   entered text
   else if (inpTextToCheck >= 0 || inpTextToCheck <= 0) {
   document.getElementById("demo").innerHTML = "The number " + inpTextToCheck + 
   " was found "
   + numOfOccurences + " times in the entered text"
   }
  //if the text to be checked is a letter and was found once in the entered text
  else if (inpTextToCheck.length == 1 && numOfOccurences == 1) {
  document.getElementById("demo").innerHTML = "The letter " + '"' + 
  inpTextToCheck + '"' + " was found once in the entered text"
  }
  //if the text to be checked is a letter and was found more than once in the 
  entered text
  else if (inpTextToCheck.length == 1 && numOfOccurences > 1) {
  document.getElementById("demo").innerHTML = "The letter " + '"' + 
  inpTextToCheck + '"' + " was found " + numOfOccurences+ " times in the entered 
  text"
  }
  //if the text to be checked is found once and is a word or letter combination
  else if (inpTextToCheck.length > 1 && numOfOccurences == 1) {
  document.getElementById("demo").innerHTML = "The word or letter combination " 
  + '"' + inpTextToCheck + '"' + " was found once in the entered text"
  }
  //if the text to be checked is found more than once and is a word or letter 
  combination
  else if (inpTextToCheck.length > 1 && numOfOccurences > 1) {
  document.getElementById("demo").innerHTML = "The word or letter combination " 
  + '"' + inpTextToCheck + '"' + " was found " + numOfOccurences + " times in 
  the entered text"
  }
  if (numOfOccurences == 0) {
  document.getElementById("demo").innerHTML = '"' + inpTextToCheck + '"' + " was 
  not found in the entered text";
    }
  }

  </script>
  </body>
  </html>

答案 2 :(得分:-1)

你需要这样做:
1.用" " NOT ""分割传递的参数 2.收集循环中字符串中每个单词的第一个字母的值,然后在循环外更新demo的html。
3.要获得拆分字符串的第一个字母,请执行spl[i].charAt(0)

function occurence(str) {
        //conversion to string to make get the value of every single letter
        var spl = str.split(" ");
        var str ="";
        //loop to subtract from spl.length to get value for chartAt
        for (i = 0; i < spl.length; i++) {
            str += spl[i].charAt(0) + "<br>";
        }
        document.getElementById("demo").innerHTML = str;
        
 }
<button onclick="occurence('The quick brown fox jumps over the lazy dog')">Go</button>
<p id="demo"></p>

答案 3 :(得分:-1)

您可以使用String.prototype.match()RegExp /\b\w/g匹配字边界,后跟一个字符,链Array.prototype.join()和参数"<br>"到结果数组。< / p>

<button onclick="occurrence('The quick brown fox jumps over the lazy dog')">Go</button>

<p id="demo"></p>

<script>
  function occurrence(str) {
    document.getElementById("demo").innerHTML = str.match(/\b\w/g).join("<br>")
  }
</script>

答案 4 :(得分:-1)

如果我找到你,你想要在字符串" "的单个单词的索引0处显示字母。

您应该在""而不是innerHTML上进行拆分,#demo的{​​{1}}应该在循环中更新而不是在外面。

我修改了您的代码,以实现我认为您正在谈论的内容。

&#13;
&#13;
function occurence(str) {
    //conversion to string to make get the value of every single letter
    var spl = str.split(" "); // split on " " not ""
    //loop to subtract from spl.length to get value for chartAt
    for (i = 0; i < spl.length; i++) {
        // innerHTML of #demo should be updated in the loop not outside
        document.getElementById("demo").innerHTML += spl[i].charAt(0) + "<br>";
    }
}
&#13;
<button onclick="occurence('The quick brown fox jumps over the lazy dog')">Go</button>

<p id="demo"></p>
&#13;
&#13;
&#13;