[英]breaking a string into different lines?
In javascript how to break my string if it exceeds 25 characters into two lines , if my string contain 75 character i want to get the string to three lines of 25 character. 在javascript中如果超过25个字符分成两行,如何打破我的字符串,如果我的字符串包含75个字符我想要将字符串变为三行25个字符。
thanks in advance 提前致谢
That's really easy to accomplish with a regular expression: 使用正则表达式很容易实现:
var text = '75 characters long (really!) — well... maybe not, but you get the picture.',
broken;
broken = text.replace(/([^\0]{25})/g, '$1\n');
As demonstrated here: http://jsbin.com/ajiyo/3 . 正如这里所示: http : //jsbin.com/ajiyo/3 。
Edit : To explain the regular expression: it will match any string of characters (the collection of every character except NUL), that is 25 characters long. 编辑 :解释正则表达式:它将匹配任何字符串(除NUL之外的每个字符的集合),即25个字符长。
The parentheses () mean that this portion should be captured , and the '$1' part of the second argument (the replacement string) refers to that first capture. 括号()表示应捕获此部分,第二个参数(替换字符串)的“$ 1”部分表示第一次捕获。
Every string of 25 characters found will be replaced by 'itself plus a newline'. 找到的每个25个字符的字符串将被'本身加上换行符'替换。 If the remainder is less than 25 characters, it will not be matched but left alone.
如果余数小于25个字符,则不会匹配,而是单独保留。
2nd edit : Brock is right, the dot loses its special meaning when in square brackets. 第二次编辑 :Brock是对的,圆点在方括号中失去了它的特殊含义。 I've replaced that by all non-NUL characters, since I don't expect NUL characters in a text string.
我用所有非NUL字符替换了它,因为我不希望文本字符串中有NUL字符。
Try to use something like this 尝试使用这样的东西
var point=0;
var myStr="12345678901234567890ABCDE my very long string 12345678901234567890ABCDE";
var myRes="";
while(myStr.substring(point).length>25)
{
myRes=myRes+myStr.substring(point,point+25)+"\n"
point+=25;
}
return myRes+myStr.substring(point);
This should get you pretty close: 这应该让你非常接近:
var txt = "This is a really long string that should be broken up onto lines of 25 characters, or less.";
for (i=0;i<(Math.ceil(txt.length/25));i++) {
document.write(txt.substring(25*i,25*(i+1)) + "<br />");
}
See working example: 见工作示例:
use str_split (php) equivalent in javascript 在javascript中使用等效的str_split(php)
http://phpjs.org/functions/str_split:530 http://phpjs.org/functions/str_split:530
function str_split (string, split_length) {
// Convert a string to an array. If split_length is specified,
// break the string down into chunks each split_length characters long.
//
// version: 1101.3117
// discuss at: http://phpjs.org/functions/str_split
// + original by: Martijn Wieringa
// + improved by: Brett Zamir (http://brett-zamir.me)
// + bugfixed by: Onno Marsman
// + revised by: Theriault
// + input by: Bjorn Roesbeke (http://www.bjornroesbeke.be/)
// + revised by: Rafał Kukawski (http://blog.kukawski.pl/)
// * example 1: str_split('Hello Friend', 3);
// * returns 1: ['Hel', 'lo ', 'Fri', 'end']
if (split_length === null) {
split_length = 1; }
if (string === null || split_length < 1) {
return false;
}
string += '';
var chunks = [], pos = 0, len = string.length;
while (pos < len) {
chunks.push(string.slice(pos, pos += split_length));
}
return chunks;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.