简体   繁体   English

字符串拆分方法(每行)javascript

[英]string split method (each in line) javascript

I'm trying to write a code for a program that allow the user to enter a word or sentnce in text-box then press the button. 我正在尝试为程序编写代码,允许用户在文本框中输入单词或句子,然后按按钮。

When the user press the button the result should appear 当用户按下按钮时,结果应显示

actually I faced a problem in last part which is split the sentence and put each in separate line. 实际上,我在最后一部分遇到了一个问题,即拆分句子并将每个句子放在单独的行中。

What I know is that I can put them in separate line by write a code such that: 我所知道的是,我可以通过编写如下代码将它们放在单独的行中:

var myString = "zero one two three four";

var mySplitResult = myString.split(" ");

for(i = 0; i < mySplitResult.length; i++){
    document.write("<br /> Element " + i + " = " + mySplitResult[i]); 
}

but when I did it, it didn't work 但是当我这样做时,它不起作用

my code 我的密码

 function t {
    var r=input.split(".");
             for (var i = 0; i < r.length; i++){
        r.innerHTML ="<br/> "  + r[i];}
          } 

You're overwriting your result.innerHTML with each iteration of the for loop 您正在用for循环的每次迭代覆盖您的result.innerHTML

Try making a var str to hold your computed string and iterate over that in the for loop. 尝试制作一个var str来保存您计算出的字符串,并在for循环中对其进行迭代。

str += "Lenrth of input string: "+ input.length +" str + =“输入字符串的长度:” + input.length +“

Number of spaces: "+num +" 空格数:“ + num +”

" +" “ +”

Replacing the word 'red' with 'blue': "+ input.replace("red" , "blue")+" 将单词“ red”替换为“ blue”:“ + input.replace(” red“,” blue“)+”

Spilting at'.': 吐在'。':

"+ " “ +”
" + n[i]; “ + n [i];

Then outside the for loop 然后在for循环之外

result.innerHTML = str result.innerHTML = str

 for (var i = 0; i < n.length; i++) { result.innerHTML = "Lenrth of input string: "+ input.length + "<p>Number of spaces: "+num +"</p>" + "<p> Replacing the word 'red' with 'blue': "+ input.replace("red" , "blue")+"</p>" + "<p>Spilting at'.':</p><br/> " + n[i]; } 

This way, you will assign to result.innerHTML and overwrite it each loop turn - so the output you will notice is only the one from the last loop turn. 这样,您将分配给result.innerHTML并在每个循环回合 result.innerHTML其覆盖-因此您将注意到的输出只是最后一个循环回合的输出。 Instead, you should build a html string part-by-part (including the repetion), and then assign the whole thing. 相反,您应该逐步构建html字符串(包括重复),然后分配整个内容。

var html = "<p>Length of input string: " + input.length + "</p>";
html += "<p>Number of spaces: " + num + "</p>";
html += "<p> Replacing the word 'red' with 'blue': " + input.replace("red" , "blue") + "</p>";
html += "<p>Spilting at '.':<ul>";
for (var i = 0; i < n.length; i++) {
    html += "<li>" + n[i] + "</li>";
}
html += "</ul></p>";
result.innerHTML = html;

Your for loop is replacing the content on each iteration. 您的for循环将替换每次迭代中的内容。 You need to make your for loop only append the elements of n to the innerHTML of the result element. 您只需要使for循环将n的元素附加到result元素的innerHTML即可。

function F1()
{  
  var str =document.getElementById("text");
  var input =  str.value;
  var result = document.getElementById( "result" );
  var num =  input.split(" ").length - 1;
  var n=input.split(".");

  result.innerHTML = "Lenrth of input string:  "+ input.length +"<p>Number of spaces:  "+num +"</p>"
  +"<p> Replacing the word 'red' with 'blue':  "+ input.replace("red" , "blue")+"</p><p>Spilting at'.':</p>";

  for (var i = 0; i < n.length; i++){
    result.innerHTML += "<br/> "  + n[i];
  }
}  

Here's a working example on jsFiddle: http://jsfiddle.net/nQ39D/ 这是jsFiddle上的一个工作示例: http : //jsfiddle.net/nQ39D/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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