我处于这样一种情况,即我希望将字符串分成两半,尊重字词以便this string here
不会被分割为this str
ing here
,而是将其分为this string
here
。
我认为一个开始步骤是将字符串拆分为基于空格的数组,然后根据这些部分计算长度,但在我的尝试中,较长的字符串最终会被错误地拆分。
答案 0 :(得分:13)
查找中间前后的第一个空格,然后选择最接近中间的空格。
示例:
var s = "This is a long string";
var middle = Math.floor(s.length / 2);
var before = s.lastIndexOf(' ', middle);
var after = s.indexOf(' ', middle + 1);
if (middle - before < after - middle) {
middle = before;
} else {
middle = after;
}
var s1 = s.substr(0, middle);
var s2 = s.substr(middle + 1);
(此代码假设中间两侧实际上有空格。您还要添加before
和after
-1
的检查。)
我在节点中谈到的检查将正确完成,如下所示:
if (before == -1 || (after != -1 && middle - before >= after - middle)) {
middle = after;
} else {
middle = before;
}
这是一个小提琴,您可以在其中编辑文本并立即查看结果:http://jsfiddle.net/Guffa/7RNBu/11/
答案 1 :(得分:3)
我想留下这个评论,但没有足够的代表点。现在最好的解决方案很容易失败,因为在使用indexOf方法时它不会检查“-1”。看到这个小提琴:
var s = "This is a long strinjjjjjjjjjjjjjjjjg";
var middle = Math.floor(s.length / 2);
var before = s.lastIndexOf(' ', middle);
var after = s.indexOf(' ', middle + 1);
if (middle - before < after - middle) {
middle = before;
} else {
middle = after;
}
var s1 = s.substr(0, middle);
var s2 = s.substr(middle + 1);
答案 2 :(得分:0)
我首先想到的是我有一个一个一个错误,但我最终解决了它。 Here's a working example.
现在打破使用的逻辑:
var calculate = function(initialString) {
var halfwayPoint = Math.floor(initialString.length / 2);
var strArray = initialString.split(' ');
// Caluclate halfway point, then break string into words
var wordFlag; // Will be split point
var charCount = 0;
_.each( strArray, function(word, strArrayIndex) {
if (wordFlag) return false;
// If we have the location, exit
// If charCount is before the halfway point
// and the end of word is after halfway point
// Then set the flag
// We add strArrayIndex to the word length to include spaces
if (charCount <= halfwayPoint &&
((charCount + word.length + strArrayIndex) >= halfwayPoint) ) {
wordFlag = strArrayIndex;
return false;
}
// Increase charCount to be length at the end of this word
charCount += (word.length);
});
if (!wordFlag) return null;
// Split the word array by the flag we figured out earlier
var lineOneArray = strArray.slice(0, (wordFlag + 1));
var lineTwoArray = strArray.slice(wordFlag + 1);
// We now join the word arrays into a string, stripping beginning and ending spaces.
var stOne = (lineOneArray.join(' ')).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
var stTwo = (lineTwoArray.join(' ')).replace(/^\s\s*/, '').replace(/\s\s*$/, '');
// Finally return the split strings as an array.
return [stOne, stTwo];
};
如果有人在我的逻辑中看到漏洞,请告诉我!我很确定这在大多数情况下都适用。
如果您希望第二个字符串长于第一个字符串(即在中间字之前而不是中间字之后断行),则不要向wordFlag添加+1。
答案 3 :(得分:0)
您可能还关心换行符,制表符以及空格,所以我会使用这样的正则表达式:
var s = "this string here";
var idx = s.length / 2;
while (idx < s.length && s[idx].match(/\s/) == null)
idx++;
var s1 = s.substring(0, idx);
var s2 = s.substring(idx);
document.getElementById("s1").innerText = s1;
document.getElementById("s2").innerText = s2;
看到这个小提琴:http://jsfiddle.net/nS6Bj/5/
答案 4 :(得分:0)
这会根据字数来分割你的字符串(不是字符数,所以每一半的确切长度可能会有很大差异,具体取决于长字和短字的位置)。
var s = "This is a string of filler text";
var pieces = s.split(" "),
firstHalfLength = Math.round(pieces.length/2),
str1 = "",
str2 = "";
for (var i = 0; i < firstHalfLength; i++){
str1 += (i!=0?" ":"") + pieces[i];
}
for (var i = firstHalfLength; i < pieces.length; i++){
str2 += (i!=firstHalfLength?" ":"") + pieces[i];
}
document.write(s);
document.write("<br />"+str1);
document.write("<br />"+str2);
// Output
This is a string of filler text
This is a string
of filler text
答案 5 :(得分:0)
let str = 'qwerty';
let half = Math.floor(str.length / 2);
str = str.slice(0, half) + ' ' + str.slice(half, str.length);
//output
'qwe rty'