0

How to split a string at a specific point defined by a number?

In example generate two variables, t1 and t2 from the string '123456' and have it split at character 3 so t1's value is '123' and t2's value is '456'...

var s0 = '123456';
console.log(s1);//123
console.log(s2);//456

6 Answers 6

1

I'd suggest:

var s0 = '123456',
    t1 = s0.substring(0, s0.indexOf(3) + 1),
    t2 = s0.substring(s0.indexOf(3) + 1);

References:

Sign up to request clarification or add additional context in comments.

5 Comments

I forgot all about substr; it should be more efficient than using a for loop, I'll accept this when the timer expires, thanks.
So what is the difference, if any, between substr and substring?
substr() allows for negative indices, whereas substring() does not. Other than that it's largely personal preference, and I prefer substring().
SO's comment/enter is really twitchy; yeah I have always used substr though I usually have to use .length on the second parameter so +1 for the extra insight.
Well, I'm glad to have helped!
1

If you meant the 3rd character:

var ch = 3;
var s0 = "123456";
var s1 = s0.substr(0,ch); // will be '123'
var s2 = s0.substr(ch);   // will be '456'

1 Comment

This is what I would have done if I had remembered substr and I did not learn the difference between substr and substring. +1 for what would have been my original approach.
1

You can just do this.

var s0 = '123456';
var arr = s0.split('3');
var t1 = arr[0] + '3', t2 = arr[1];

5 Comments

I answered my own question for the sake of others and my solution doesn't use regex.
I thought of this approach, but then discarded it because it seems that John wants to split based on the position of the '3' character, which isn't necessarily the third character in the sequence. Having said that, in his own answer it does seem to be the third character...so, who knows?
I'm dealing with Firefox's copying of text/html that could be used to easily paste adult website content to a contact form so I'm mitigating that by handling the pasted content as text instead (so images don't easily show up from such sites). The number 3 is arbitrary, from anchor offset.
@DavidThomas edited thanks. Thought he wanted to divide it into equal strings
@AmitJoki Nope, I should have tried a longer string with an odd split (like 57%) though I wanted to keep it simple; thanks for sharing your misperception because now I can take that in to account when posting a question in the future, +1.
1

Something like:

var foo = '123456'
   ,bar = [foo.slice(0,3), foo.slice(3)];
//=> bar now ["123", "456"]

Extend the String prototype:

String.prototype.splitAt = function(n) {
  return n && n < this.length 
         ? [this.slice(0,n), this.slice(n)] 
         : this;
}
// usages
'123456'.splitAt(3);  //=> ['123', '456']
'123456'.splitAt(2);  //=> ['12', '3456']
'123456'.splitAt(12); //=> '123456'
'123456'.splitAt();   //=> '123456'

1 Comment

+1 for brevity (plus I can replace the '3' with the object containing the dynamic value which changes easily versus regex) though I'm going with the substring answer I'm sure that this will fit better for others regardless. Plus I've got to break in to prototype at some point...
0

Try

var s0 = "123456"
, s1 = s0.slice(0, 3); // first 3 characters in string , `123`
, s2 = s0.slice(- (s0.length - s1.length) ); // remainder of string , `456`(+)
console.log(s0, s1, s2)

Comments

-1
var s = '123456';
var sos = 3;//number to split by
var t1 = '';
var t2 = '';

for (var i = 0; i < s.length; i++)
{
 if (i<sos) {t1 += s[i];}
 else {t2 += s[i];}
}

console.log('t1 = '+t1);
console.log('t2 = '+t2);

2 Comments

You can immediately answer your own questions to share your knowledge. In this instance it also produced a better result thanks to substr which I completely forgot about.
you could have put that in question and asked for a better approach.. Nevertheless, just don't mind my comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.