最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to split long string after first space after 100 characters - Stack Overflow

programmeradmin1浏览0评论

I have a long string and want to split it into segments of +/- 100 characters if it has more than 100 otherwise not. 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.

I'm clueless how to do that with .split()

I have a long string and want to split it into segments of +/- 100 characters if it has more than 100 otherwise not. 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.

I'm clueless how to do that with .split()

Share Improve this question asked May 23, 2014 at 11:07 gcogco 1,7007 gold badges24 silver badges46 bronze badges 2
  • 3 Hint: don't split, try to match. Here's a start /[\s\S]{1,100}\S*/g – HamZa Commented May 23, 2014 at 11:14
  • 1 You can't do that only with split(). split is not the good method here. – Casimir et Hippolyte Commented May 23, 2014 at 11:14
Add a ment  | 

5 Answers 5

Reset to default 5

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:

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.

You can find the first space after position 100 as:

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.

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.
}

How about string.split(/(.{100}\S*)\s/).filter(function(e){return e;});

As explained in the documentation, you can use a RegExp with split, and capture a part of it using capture groups.

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 :

s.split(/(.{100}[^\s]*)\s/).filter(function(s) {
    return s.length > 0;
});

You can easily do that using 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)
发布评论

评论列表(0)

  1. 暂无评论