2015-10-08 76 views
0

我有字符串:“这是一个示例字符串”,我需要将它拆分为2个字符串,而不会打破单词,并且两个字符串的长度最接近,这样的结果将是:将字符串拆分为两部分,将具有几乎相同的长度

["This is a", "sample string"]. 

另一个前:

"Gorge is nice" => ["Gorge", "is nice"] 

而且这将是很好,如果该函数可以得到尽可能PARAM,我会得到的结果的要素的数量。

感谢您的帮助!

+0

你可以像这样[链接]计字(http://stackoverflow.com分路器/ questions/18679576 /计数字符串) 和t母鸡拆分文本。 –

+0

计数词,然后在总词的一半处分割,在空白处 – mgul

+0

ziki查看[Demo using substr](https://jsfiddle.net/tusharj/9ub0erLp/)另请参阅[使用数组](https: //的jsfiddle。net/tusharj/9ub0erLp/1 /) – Tushar

回答

3

您可以使用indexOf并将第二个参数作为字符串长度的一半。由此,indexOf将在所提供的索引之后搜索匹配字符串的下一个索引

Demo

var str = "Thisasdfasdfasdfasdfasdf is a sample string", 
 
    len = str.length; 
 

 
var ind = str.indexOf(' ', Math.floor(str.length/2) - 1); 
 
ind = ind > 0 ? ind : str.lastIndexOf(' '); 
 

 
var str1 = str.substr(0, ind), 
 
    str2 = str.substr(ind); 
 

 
document.write(str1 + ' <br /> ' + str2);


UPDATE

如果我需要将其拆分为3或4或什么元素?

function split(str, noOfWords) { 
 
    // Set no. of words to 2 by default when nothing is passed 
 
    noOfWords = noOfWords || 2; 
 

 
    var len = str.length; // String length 
 
    wordLen = Math.floor(len/noOfWords); // Approx. no. of letters in each worrd 
 

 
    var words = [], 
 
    temp = '', 
 
    counter = 0; 
 

 
    // Split the string by space and iterate over it 
 
    str.split(' ').forEach(function(v) { 
 
    // Recalculate the new word length 
 
    wordLen = Math.floor((len - words.join(' ').length)/(noOfWords - counter)); 
 

 
    // Check if word length exceeds 
 
    if ((temp + v).length < wordLen) { 
 
     temp += ' ' + v; 
 
    } else { 
 
     // Add words in the array 
 
     words.push(temp.trim()); 
 

 
     // Increment counter, used for word length calculation 
 
     counter++; 
 
     temp = v; 
 
    } 
 
    }); 
 

 
    // For the last element 
 
    temp.trim() && words.push(temp.trim()); 
 
    return words; 
 
} 
 

 
var str = "This is a sample string. Lorem ipsum dolor sit amet, consectetur adipisicing elit. Eos quae error ab praesentium, fugit expedita neque ex odio veritatis excepturi, iusto, cupiditate recusandae harum dicta dolore deleniti provident corporis adipisci."; 
 

 
var words = split(str, 10); 
 

 
console.log(words); 
 
document.write('<pre>' + JSON.stringify(words, 0, 2) + '</pre>');

+0

http://jsfiddle.net/arunpjohny/eorp20pu/1/? –

+0

@ArunPJohny感谢您指出,添加'ind = ind> 0? ind:str.lastIndexOf('');'将解决问题 – Tushar

+0

@Tushar不是真的http://jsfiddle.net/arunpjohny/eorp20pu/2/ –

-2

可以通过空间

例如为:

var words = 'George is nice' 
words = words.split(' ') 

分割你的话,然后步行通过数组和比较字符串长度。 作为例子

var concat = '' 
for (var word in words) 
{ 
    word = words[word] 
    if (word.length < words[word - 1]) 
    { 
     concat += word 
    } 
} 

这将是很好知道为什么它必须是只有2个元素,而不是单词量? 你打算使用一些算法吗?或者你只是想将最少量的文本分组在一起?

这将是很高兴看到你为什么需要这个。

+0

我需要将它们保存在两个地方才能显示它们,但它们的长度相同 – Ziki

1

你可以尝试像

function split(str) { 
 
    var len = str.length, 
 
    mid = Math.floor(len/2); 
 

 
    var left = mid - str.substring(0, mid).lastIndexOf(' '), 
 
    right = str.indexOf(' ', mid) - mid; 
 

 
    var ind = mid + (left < right ? -left : right); 
 

 
    return [str.substr(0, ind), 
 
    str2 = str.substr(ind) 
 
    ]; 
 
} 
 

 
var parts = split("123-456 3-5-asldfkjas kdfasdfasd fjas"); 
 
snippet.log(parts[0]); 
 
snippet.log(parts[1]);
<!-- Provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 --> 
 
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

+0

http://jsfiddle.net/arunpjohny/eorp20pu/ –