Javascript 如何使用 jQuery 在每 200 个字符后插入一个换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4321500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert a newline character after every 200 characters with jQuery
提问by Nishant Kumar
I have a textbox that I use to insert some information. I want to insert a newline character after every 200 characters using jQuery or JavaScript.
我有一个文本框,用于插入一些信息。我想使用 jQuery 或 JavaScript 在每 200 个字符后插入一个换行符。
For instance:
例如:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
In this example, I want to insert a newline chracter after every 200 characters. How can I do this?
在这个例子中,我想在每 200 个字符后插入一个换行符。我怎样才能做到这一点?
回答by casablanca
Break the string after every 200 characters, add a newline, and repeat this process with the remaining string:
每 200 个字符后打破字符串,添加一个换行符,并用剩余的字符串重复此过程:
function addNewlines(str) {
var result = '';
while (str.length > 0) {
result += str.substring(0, 200) + '\n';
str = str.substring(200);
}
return result;
}
回答by Martin K
You can do this is just one line of code.
您只需一行代码就可以做到这一点。
var newStr = str.replace(/.{200}/g, "var newStr = str.replace(/.{200}/g, "prefix- function addBreaks(s,c) {
var l = s.length;
var i = 0;
while (l > c) {
l = l-c;
i=i+c;
s = s.substring(0,c)+"\n"+s.substring(c);
}
return s;
}
var a=' aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';
addBreaks(a,200);
-postfix\n")
\n")
Works well if you want prefixes or postfixes on every line as well
如果您还希望在每一行都使用前缀或后缀,则效果很好
$("#text").keyup(function()//Detect keypress in the textarea
{
var text_area_box =$(this).val();//Get the values in the textarea
var max_numb_of_words = 200;//Set the Maximum Number of chars
var main = text_area_box.length*100;//Multiply the lenght on words x 100
var value= (main / max_numb_of_words);//Divide it by the Max numb of words previously declared
if(text_area_box.length >= max_numb_of_words) {
//add break
}
return false;
});
回答by Tom Belote
Try this function it is just plain javascript. It takes the string and the number of characters to break after.
试试这个功能,它只是普通的 javascript。它需要字符串和字符数来打破。
##代码##回答by alxndr
回答by kobe
If user is entering that text you can do the following:
如果用户正在输入该文本,您可以执行以下操作:
Implement keydown event and whenever you reach the 200 count... insert a new line character and append it again.
If you are getting it from database, just before setting the value read that string and insert a new line character manually.
实现 keydown 事件,每当达到 200 计数时...插入一个新行字符并再次追加它。
如果您是从数据库中获取它,则在设置值之前读取该字符串并手动插入一个新行字符。