简体   繁体   English

如何在100个字符后的第一个空格后拆分长字符串

[英]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: 例如,这意味着我有以下字符串:

string input 字符串输入

"asld asö öasdn askdn asklcanscklans cakslcna acsklcnasclk ncaslkcnas casld asö öasdn askdn asklcanscklans cakslcna acsklcnasclk ncaslkcnas c"

and need these 2 strings out of it 并需要这两个字符串

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.

相关问题 如何在 15 个字符后的第一个空格后拆分 JavaScript 中的字符串? - How can I split a string in JavaScript after the first space after 15 characters? 在 Javascript 中的一定数量的字符之后在空格处拆分字符串 - Split string at space after certain number of characters in Javascript 如何在javascript中的一定数量的字符后在空格中拆分字符串? - How do I split a string at a space after a certain number of characters in javascript? 如何在第一个数字之后添加空格(JavaScript,.split) - How to add a space after first number (Javascript, .split) 在Javascript中的一定数量的字符之后,在空格或“/”或最大字符之后分割字符串 - Split string at space or "/" or after max character after certain number of characters in Javascript 如何在给定字符串之前和之后替换除100个字符以外的所有内容? - How to replace everything except 100 characters before and after a given string? 如何在 javascript 中的 30 个数组之后用空格分割字符串? - How do I split a string with space after 30 array in javascript? 如何按空格分割字符串,即不由指定字符包装的字符串? - How to split string by space, that is not wrapped by specified characters? 按空格分割字符串,但忽略引号中的某些单词和空格后的空格 - Split string by space, but ignore space after some words and space in quotes jQuery字符串使用split()方法在空格后拆分字符串 - jQuery string split the string after the space using split() method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM