我在这里有一个有趣的问题。我有一些JavaScript字符串,如下:
var a = "A few sentences. For tests. The point of these sentences is to be used as examples.";
var b = "A couple more sentences. These ones are shorter.";
var c = "Blah. Foo. Bar. Baz. Test. Test 2. Test C.";
var d = "Test sentence.";
我希望扩展
string
原型,使其具有将每个字符串拆分为两个字符串的方法,每个字符串中的字符数量尽可能相似,同时保持整个句子不被拆开。我需要的结果是:
a.halve() // ["A few sentences. For tests.", "The point of these sentences is to be used as examples."]
b.halve() // ["A couple more sentences.", "These ones are shorter."]
c.halve() // ["Blah. Foo. Bar. Baz.", "Test. Test 2. Test C."]
d.halve() // ["Test sentence.", ""]
如果我执行 a.length / 2
,则可以获得两个字符串的理想目标长度... 我只是在尝试正确顺序下进行 split
和 join
。
split()
是用来按照分隔符字符进行分割的,而不是位置。请使用substr()
。 - Barmar?
和!
作为句子结尾?引用句号如何处理,例如他说:“救命啊!”我说:“我来了。”
? - Barmar