简体   繁体   English

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

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

These strings may be long paragraphs, so I'm not sure it's best to split the entire string with a space delimiter. 这些字符串可能是长段落,所以我不确定最好用空格分隔符分割整个字符串。 I'm trying to get, say, the first 10 words and wrap them in a span: 我试着得到前10个单词并将它们包裹在一个范围内:

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

Then rejoin that with the second half of the original split. 然后用原始分割的后半部分重新加入。 Suggestions on a super efficient way to do this? 建议以超高效的方式做到这一点? It would affect at most three paragraphs on the page at a time. 它一次最多会影响页面上的三个段落。

EDITED EDITED

Here's a kicker — The split should occur after the 9th word OR at the end of the first sentence (if that sentence is less than 9 words). 这是一个踢球者 - 分裂应该发生在第一个单词的第9个单词OR之后(如果那个句子小于9个单词)。

EXAMPLE

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.';

Or with a short sentence that starts the paragraph: 或者用一个简短的句子开始段落:

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.';

Considering you are only going to be scanning at most about 100 chars (unless you have URIs or very long words) then scanning character by character is quite optimal. 考虑到你最多只扫描100个字符(除非你有URI或很长的单词),然后逐个字符扫描是非常理想的。 You could optimise this by using .indexOf() in certain places, but you'd loose what you gained in having to check for each different character that could terminate a sentence. 你可以在某些地方使用.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 );

(The above code assumes your text will always be correctly gramatically termintated (ie contain at least one of ?!.;:) - if not then it would be possible for a paragraph with less than 9 words to end up spanless. This could be fixed by a few changes however...) (上面的代码假设你的文本总是被正确地格式化终止(即至少包含一个?!。; :) - 如果没有,那么一个少于9个单词的段落最终可能无法完成。这可能是然而,由一些变化修复...)

note for future readers 为未来的读者留意

If you're going for a 'super efficient' way of doing string searching avoid Regular Expressions (unless you really need their power) . 如果你想要一种“超级高效”的字符串搜索方式,请避免使用正则表达式(除非你真的需要它们的力量) The accepted answer for this question is concise and nicely put together function - don't get me wrong - but it's about 70% slower than just scanning the string with a for loop (in my tests on FireFox & Chrome at least) ... and that's even when comparing after moving the Regular Expression definitions outside of Bergi's function (ie using pre-compiled regexps rather than recreating them every time the function is called) . 这个问题的接受答案很简洁,很好地把功能放在一起 - 不要误解我的意思 - 但它比用for循环扫描字符串慢了大约70% (至少在我的FireFox和Chrome测试中) ......甚至在将正则表达式定义移到Bergi函数之外时进行比较(即使用预编译的正则表达式而不是每次调用函数时重新创建它们)

http://jsperf.com/compare-regexp-vs-char-scanning 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(" ");
});

This matches the first sentence-like thing (or the whole string - unless linebreaks), and wraps the first 10 words of it in that span. 这匹配第一个类似于句子的东西(或整个字符串 - 除非换行符),并在该跨度中包装它的前10个单词。 Works for both your sample inputs, but also on smaller ones. 适用于您的样本输入,也适用于较小的输入。 Returns the empty string for an empty string, change the regex to …|.*$ if you want an empty span. 返回空字符串的空字符串,如果想要空字符,请将正则表达式更改为…|.*$

How about this code: 这段代码怎么样:

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'));

I have it under fiddle so you can test. 我把它放在小提琴下,这样你就可以测试了。 You would want to do this in a loop with the remainder of arr1. 您可能希望在循环中使用arr1的其余部分执行此操作。

Update: 更新:

If it's not just the full stop but also ?!:;etc. 如果它不仅仅是完全停止而且还是?!:;等等。 then create a RegExp and test instead of doing lastIndexOf('.') 然后创建一个RegExp并测试而不是执行lastIndexOf('.')

Here. 这里。 It's a bit code-golfy though. 虽然它有点代码高尔夫球。 Sorry. 抱歉。

$( '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 );  
});

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

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

相关问题 如何将一个长的连续字符串拆分为它包含的单词数组? - How can I split a long continuous string into an array of the words it contains? 如何在100个字符后的第一个空格后拆分长字符串 - How to split long string after first space after 100 characters 如何在JavaScript中按n个字符分割字符串? - How might I split a string by every nth character in JavaScript? 如何根据两个分隔符拆分字符串? - How can I split a string based on two delimiters? 如何在Javascript中将一个字符串拆分为两个浮点数? - How can I split a string into two float numbers in Javascript? 如果有足够的空间,如何将div列表分为两列? - How can I split a list of divs into two columns if there's enough space? 第n个字符出现后如何拆分字符串? - How to split a string after the nth occurence of a character? 如何在 15 个字符后的第一个空格后拆分 JavaScript 中的字符串? - How can I split a string in JavaScript after the first space after 15 characters? 如何使用从特定字符开始的 JavaScript 将字符串拆分为单词,然后在下一个空格处结束? - How can I split a string into words with JavaScript starting at a particular character, and then end at the next space? 从字符jquery的第n次出现将字符串拆分为两个子字符串 - split string to two substrings from nth occerence of a charactor jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM