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
John
  • 1
  • 13
  • 98
  • 177

6 Answers6

1

You can just do this.

var s0 = '123456';
var arr = s0.split('3');
var t1 = arr[0] + '3', t2 = arr[1];
Amit Joki
  • 58,320
  • 7
  • 77
  • 95
  • I answered my own question for the sake of others and my solution doesn't use regex. – John Oct 19 '14 at 15:03
  • 1
    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? – David Thomas Oct 19 '14 at 15:05
  • 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. – John Oct 19 '14 at 15:09
  • @DavidThomas edited thanks. Thought he wanted to divide it into equal strings – Amit Joki Oct 19 '14 at 15:13
  • @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. – John Oct 19 '14 at 15:19
1

I'd suggest:

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

References:

David Thomas
  • 249,100
  • 51
  • 377
  • 410
  • I forgot all about `substr`; it should be more efficient than using a `for` loop, I'll accept this when the timer expires, thanks. – John Oct 19 '14 at 15:05
  • So what is the difference, if any, between `substr` and `substring`? – John Oct 19 '14 at 15:11
  • [`substr()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/substr) allows for negative indices, whereas `substring()` does not. Other than that it's largely personal preference, and I prefer `substring()`. – David Thomas Oct 19 '14 at 15:13
  • 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. – John Oct 19 '14 at 15:16
  • Well, I'm glad to have helped! – David Thomas Oct 19 '14 at 15:17
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'
KooiInc
  • 119,216
  • 31
  • 141
  • 177
  • +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... – John Oct 19 '14 at 15:21
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'
Dustin Hoffner
  • 2,025
  • 1
  • 14
  • 15
  • 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. – John Oct 19 '14 at 15:17
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)
guest271314
  • 1
  • 15
  • 104
  • 177
-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);
John
  • 1
  • 13
  • 98
  • 177
  • 1
    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. – John Oct 19 '14 at 15:07
  • you could have put that in question and asked for a better approach.. Nevertheless, just don't mind my comment – Amit Joki Oct 19 '14 at 15:10