1
var string = abc123;
string.split(*POSITION=3)???

Is there a way to split this string into an array of strings that is [abc,123]? Is the split method the right thing to use?

1
  • 1
    split() needs a delimiter to work. Commented Nov 28, 2011 at 19:13

4 Answers 4

2

I would say the split method is not the right thing to use here, as its purpose is to segment a string based on a certain character.

You could certainly write your own function, of course:

function partition(str, index) {
  return [str.substring(0, index), str.substring(index)];
}

// results in ["123", "abc"]
var parts = partition("123abc", 3);

If you wanted to write "123abc".partition(3) instead, you could make that possible by extending String.prototype:

String.prototype.partition = function(index) {
  return [this.substring(0, index), this.substring(index)];
};

Personally, though, I'd recommend avoiding that sort of tomfoolery (search the web for "extending built-in objects in JavaScript" if you want to read what others have to say on the topic).

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

8 Comments

Just curious (because I see this time to time), if you don't recommend doing it by extending the prototype, why not do it in the way you do recommend? e.g.: function partionString(str, index) {...
I highly doubt this is what the OP wants... "hello friend".partition(3) ["hel", "lo friend"] doesn't split *POSITION=3 at all for me. Then again, *POSITION=3 is very open to interpretation :D Just putting it out there.
@rob: Totally fair point; I've updated my answer accordingly.
@Esailija: It certainly is open to interpretation... I see, your interpretation was that he wants to split the string up into an arbitrary number of segments of equal length? My answer reflects what I think he wants: to cut a string in two parts around a specified position. But you're right; it's very possible I've misconstrued the OP's intent.
@DanTao, I mean like if you have a string "FFFFFF" and then do *POSITION=2 split, you will end up with ["FF", "FF", "FF"] (Split every 2 characters), which is the kind of functionality php str_split does. But looks like he wasn't looking for that after all :P
|
1

Maybe use a simple RegExp match?

var arr = "abc123".match(/^([a-z]+)(\d+)$/i);
arr.shift();

console.log(arr);

Comments

1
var arr = "this is a test".match(/.{1,3}/g);

Comments

0

No but it is trivial to implement that:

function splitn( str, n ){
var r = [], offset = 0, l = str.length;

    while( offset < l ) {
    r.push( str.substr( offset, n ) );
    offset += n;
    }
return r;
}

Then:

var string = "abc123";
console.log( splitn( string, 3 ) );
//["abc", "123"]

Assuming you want similar functionality to str_split

Comments

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.