拆分文本以每 n 个字符添加一个新行,以处理空格 [英] Split a text to add a new line every n characters taking care of spaces

查看:22
本文介绍了拆分文本以每 n 个字符添加一个新行,以处理空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为警报和确认对话框编写文本时的常见问题:在添加换行符之前要键入的字符数.一些浏览器会在某一时刻自动中断,而另一些则在其他时候自动中断.所以你只能猜测了.有用的代码片段是一个 Javascript 函数,它将警报或确认对话框的预期文本和字符长度作为输入,然后返回相同的输入字符串,仅在最接近传入字符长度的空格的位置添加新行字符.这样,单词不会中途分解.

Common problem when writing text for alert and confirm dialogs: number of characters to type before adding a newline character. Some browsers auto-break at one point, others at others. So you are left guessing. Useful snippet is a Javascript function that takes as input the alert or confirm dialog's intended text and a char length, then returns the same input string only with new line chars added at the positions with spaces closest to the char length passed in. That way, words are not broken up mid-way.

示例:
1. 为要用于警报或确认对话框的文本字符串分配一个 var,例如:
var a = "我的狗有跳蚤,敏捷的棕色狐狸跳过懒惰的狗等";
2.通过函数运行文本,例如:
a = breakLines(a);//默认,每 50 个字符中断一次

a = breakLines(a, 20);//每 20 个字符中断一次

在通过函数运行后显示 'a' 的值,您将看到在最接近您指定的字符位置的空格字符处指定的每个位置都添加了换行符.例如,如果您指定 20,'a' 将转换为以下内容:

Example:
1. Assign a var to a string of text you want to use for the alert or confirm dialog box, eg.:
var a = "My dog has fleas the quick brown fox jumps over the lazy dog etc";
2. Run the text through the function, for example:
a = breakLines(a); // Default, break every 50 chars
or
a = breakLines(a, 20); // Break every 20 chars

Display the value of 'a' after running it through the function and you will see line breaks have been added at every place you specified at the space character closest to the character place you specified. For example if you specified 20, 'a' would be converted to the following:

'我的狗有跳蚤\n敏捷的棕色狐狸\n跳过懒惰的\n狗等'

'My dog has fleas\nthe quick brown fox\njumps over the lazy\ndog etc'

对于字符串中的每一行(一行是以换行符结尾的字符串的一部分),该行的两边都被删掉了空格.下面的代码片段使用 jQuery $.trim() 函数来执行此操作,但还有其他方法可以在不使用 jQuery 库的情况下执行此操作,例如使用 regexp.只需根据您想使用的替代方法修改代码即可.

For each line in the string (a line is a portion of the string ending in a new line character), the line is trimmed of whitespace on both sides. The code snippet below uses the jQuery $.trim() function to do this but there are other ways to do so without using the jQuery library, such as with regexp. Just modify the code as you want to to use alternate means.

这引出了我的问题:除了按照我已经完成的方式做我想做的事情,如下所示,是否有更简单更紧凑的方式来做,例如可以利用正则表达式?有吃货吗?

This leads to my question: Aside from doing what I want to do the way I have done it as shown below, is there an easier more compact way of doing it, something that for example can leverage regexp? Any takers?

function breakLines(text, linelength)
{
 var linebreak = '\n';
 var counter = 0;
 var line = '';
 var returntext = '';
 var bMatchFound = false;
 var linelen = 50; // 50 characters per line is default

 if(linelength)
 {
  linelen = linelength;
 }

 if(!text){ return '';}
 if(text.length < linelen+1) { return $.trim(text);}

 while(counter < text.length)
 {
  line = text.substr(counter,linelen);
  bMatchFound = false;
  if (line.length == linelen)
  {
   for(var i=line.length;i > -1;i--)
   {
    if(line.substr(i,1)==' ')
    {
     counter += line.substr(0,i).length;
     line = $.trim(line.substr(0,i)) + linebreak;
     returntext += line;
     bMatchFound = true;
     break;
    }
   }

   if(!bMatchFound)
   {
    counter+=line.length;
    line = $.trim(line) + linebreak;
    returntext += line;
   }
  }
  else
  {
   returntext += $.trim(line);
   break; // We're breaking out of the the while(), not the for()
  }
 }

 return returntext;
}

推荐答案

无参数验证的简短版本

function explode(str, maxLength) {
    var buff = "";
    var numOfLines = Math.floor(str.length/maxLength);
    for(var i = 0; i<numOfLines+1; i++) {
        buff += str.substr(i*maxLength, maxLength); if(i !== numOfLines) { buff += "\n"; }
    }
    return buff;
}

这篇关于拆分文本以每 n 个字符添加一个新行,以处理空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆