繁体   English   中英

如何在第n个空格中将长字符串拆分为两个?

[英]How can I split a long string in two at the nth space?

这些字符串可能是长段落,所以我不确定最好用空格分隔符分割整个字符串。 我试着得到前10个单词并将它们包裹在一个范围内:

'<span class="easing">' + string + '</span>'

然后用原始分割的后半部分重新加入。 建议以超高效的方式做到这一点? 它一次最多会影响页面上的三个段落。

EDITED

这是一个踢球者 - 分裂应该发生在第一个单词的第9个单词OR之后(如果那个句子小于9个单词)。

var origString = 'Coming into the world on Elvis’ birthday with a doctor named Presley seemed fortuitous until, wielding the silvery smooth scalpel in his aged unsteady hand, the doctor sliced through the walls of my mother’s uterus and into my unborn skin. Inside the warm soothing waters of my mother’s womb, inside the silent weightlessness, I was safe. Then the prick of cold steel marked the first in a series of rude awakenings. I was scarred for life even before birth.';
var newString = '<span="easing">Coming into the world on Elvis’ birthday with a doctor</span> named Presley seemed fortuitous until, wielding the silvery smooth scalpel in his aged unsteady hand, the doctor sliced through the walls of my mother’s uterus and into my unborn skin. Inside the warm soothing waters of my mother’s womb, inside the silent weightlessness, I was safe. Then the prick of cold steel marked the first in a series of rude awakenings. I was scarred for life even before birth.';

或者用一个简短的句子开始段落:

var origString = '“Is he okay? Tell me everything’s okay” she pleas, her desperate need to confirm my health competing with her own need for consolation.';
var newString = '<span class="easing">“Is he okay?</span> Tell me everything’s okay” she pleas, her desperate need to confirm my health competing with her own need for consolation.';

考虑到你最多只扫描100个字符(除非你有URI或很长的单词),然后逐个字符扫描是非常理想的。 你可以在某些地方使用.indexOf()来优化它,但是你必须在检查可以终止句子的每个不同角色时获得你所获得的。

function spanomatic ( str, words ) {
  var i, l, c;
  for ( i=0, l=str.length; i<l; i++ ) {
    c = str.charAt(i);
    if ( c == ' ' ) {
      if ( words-- <= 0 ) {
        str = '<span>'+str.substring(0,i)+'</span>'+str.substring(i);
        break;
      }
    }
    else if ( ('?!.;:').indexOf(c) != -1 ) {
      str = '<span>'+str.substring(0,i)+'</span>'+str.substring(i);
      break;
    }
  }
  return str;
}

spanomatic ( 'Pass your string here', 9 );

(上面的代码假设你的文本总是被正确地格式化终止(即至少包含一个?!。; :) - 如果没有,那么一个少于9个单词的段落最终可能无法完成。这可能是然而,由一些变化修复...)

为未来的读者留意

如果你想要一种“超级高效”的字符串搜索方式,请避免使用正则表达式(除非你真的需要它们的力量) 这个问题的接受答案很简洁,很好地把功能放在一起 - 不要误解我的意思 - 但它比用for循环扫描字符串慢了大约70% (至少在我的FireFox和Chrome测试中) ......甚至在将正则表达式定义移到Bergi函数之外时进行比较(即使用预编译的正则表达式而不是每次调用函数时重新创建它们)

http://jsperf.com/compare-regexp-vs-char-scanning

return string.replace(/.+?[,.?!]|.+$/, function(match, index, string){
    var words = match.split(/\s+/);
    words[ words.length<10 ? words.length-1 : 9 ] += '</span>';
    return '<span class="easing">' + words.join(" ");
});

这匹配第一个类似于句子的东西(或整个字符串 - 除非换行符),并在该跨度中包装它的前10个单词。 适用于您的样本输入,也适用于较小的输入。 返回空字符串的空字符串,如果想要空字符,请将正则表达式更改为…|.*$

这段代码怎么样:

var str = 'asda adsfadsf asdfadfadsf adsfsdafadf. adfadfadfad adfadfdaf adfadfadf adfadf \afsgasfggasfg SFGDFGDSFGH dfghdsghdgas hadghdagh';

var sentences = [], words = str.split(' ');
for (var i = 0; i < 9; i++) {
    if (words[i].lastIndexOf('.') !== -1) {
        sentences.push(words[i]);
        break;    
    } else {
        sentences.push(words[i]);
    }        
}

words.slice(sentences.length, words.length);


$('<span>' + sentences.join(' ') + '</span>').appendTo($('#log'));

我把它放在小提琴下,这样你就可以测试了。 您可能希望在循环中使用arr1的其余部分执行此操作。

更新:

如果它不仅仅是完全停止而且还是?!:;等等。 然后创建一个RegExp并测试而不是执行lastIndexOf('.')

这里。 虽然它有点代码高尔夫球。 抱歉。

$( 'p' ).html(function ( i, text ) {
    var re = /(.+?)\s/g, c = 0, res;   
    while ( res = re.exec( text ) ) if ( ++c === 10 || res[1].slice( -1 ) === '.' ) break;

    var p = re.lastIndex;
    return '<span class="easing">' + text.slice( 0, p ) + '</span>' + text.slice( p );  
});

现场演示: http //jsfiddle.net/3DaEV/

暂无
暂无

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

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