[英]How to split long string after first space after 100 characters
I have a long string and want to split it into segments of +/- 100 characters if it has more than 100 otherwise not. 我有一个长字符串,如果要超过100个字符,则想将其拆分为+/- 100个字符的段。 That means I have for example this string:
例如,这意味着我有以下字符串:
"asld asö öasdn askdn asklcanscklans cakslcna acsklcnasclk ncaslkcnas casld asö öasdn askdn asklcanscklans cakslcna acsklcnasclk ncaslkcnas c"
1: "asld asö öasdn askdn asklcanscklans cakslcna acsklcnasclk ncaslkcnas casld asö öasdn askdn asklcanscklans"
2: "cakslcna acsklcnasclk ncaslkcnas c"
As you can see, I need to split them after the first space, after the 100th character. 如您所见,我需要在第一个空格之后,第100个字符之后分割它们。
I'm clueless how to do that with .split() 我不知道如何使用.split()
You can use indexOf to search your string for a specified character, indexOf also provides an optional argument for a starting index in your string, giving you the position from which you can substring or do whatever you need to do: 您可以使用indexOf在字符串中搜索指定的字符,indexOf还为字符串中的起始索引提供了可选参数,为您提供了可以从其子字符串或执行任何操作的位置:
string.indexOf(' ',100);
If you want to perform further splits you can use a loop to run back over the results and perform the same action. 如果要执行进一步的拆分,则可以使用循环来遍历结果并执行相同的操作。
There are some suggestions of using regex here which may be fine, all I would say is if you want to use Regex then go away and get an understanding of what the pattern does. 这里有一些使用正则表达式的建议可能会很好,我要说的是,如果您想使用正则表达式,那么请走开并了解该模式的作用。 Using code you don't understand makes your life difficult later on.
使用您不理解的代码会使您以后的生活变得困难。
怎么样string.split(/(.{100}\\S*)\\s/).filter(function(e){return e;});
You can find the first space after position 100 as: 您可以在位置100之后找到第一个空格,如下所示:
var index = string.indexOf(' ', 100);
You want to split after this space, so your second part starts at: 您想在此空间之后拆分,因此第二部分从以下位置开始:
var splitPoint = string.indexOf(' ', 100) + 1;
If the string is not 100 characters long, or there is no space after position 100, then splitPoint
is zero (because indexOf
returns -1), and you don't want to split. 如果字符串的长度不是100个字符,或者位置100之后没有空格,则
splitPoint
为零(因为indexOf
返回-1),并且您不想拆分。
So you can split the string as: 因此,您可以将字符串拆分为:
if (splitPoint > 0) {
var part1 = string.substring(0, splitPoint);
var part2 = string.substring(splitPoint);
// Do something with the parts.
}
As explained in the documentation , you can use a RegExp
with split, and capture a part of it using capture groups. 如文档中所述,您可以将
RegExp
与split一起使用,并使用捕获组捕获其中的一部分。
var s = "asld asö öasdn askdn asklcanscklans cakslcna acsklcnasclk ncaslkcnas casld asö öasdn askdn asklcanscklans cakslcna acsklcnasclk ncaslkcnas c";
s.split(/(.{100}[^\s]*)\s/);
// 1 : "asld asö öasdn askdn asklcanscklans cakslcna acsklcnasclk ncaslkcnas casld asö öasdn askdn asklcanscklans"
// 2 : "cakslcna acsklcnasclk ncaslkcnas c"
And to remove the empty element(s), you can use the filter
method : 要删除空元素,可以使用
filter
方法:
s.split(/(.{100}[^\s]*)\s/).filter(function(s) {
return s.length > 0;
});
You can easily do that using substr 您可以使用substr轻松做到这一点
var your_string = "asld asö öasdn askdn asklcanscklans cakslcna acsklcnasclk ncaslkcnas casld asö öasdn askdn asklcanscklans cakslcna acsklcnasclk ncaslkcnas c"
var string1 = your_string.substr(0,105);
var string2 = your_string.substr(106,your_string.length)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.