Coming from PHP, I’m well versed in using the handy explode function to force a string into an array, using a specified delimiter character to chop up the string into the little bits that are to be stored in the array.
Happily for me, plain old vanilla JavaScript also has this ability built into the language, choosing to call its version of this functionality “split”.
From the official definition: The split() method is used to split a string into an array of substrings, returning the new array containing the substrings.
It accepts two parameters, namely the separator and the limit, both of which are actually optional by the way. The separator parameter is where you specify the delimiter character (if you omit this, the entire string will be stuffed into a single element array ). The optional limit parameter if set will control the number of splits.
A few examples on using split:
var str="How are you doing today?";
//some split usage examples
document.write(str.split() + "<br />");
document.write(str.split(" ") + "<br />");
document.write(str.split("") + "<br />");
document.write(str.split(" ",3));
//the output (',' indicates array boundary)
//How are you doing today?
//How,are,you,doing,today?
//H,o,w, ,a,r,e, ,y,o,u, ,d,o,i,n,g, ,t,o,d,a,y,?
//How,are,you
Useful.
Coming from PHP, I'm well versed in using the handy implode function to flatten an array into a string, specifying the delimiter character to be used to indicate the border between the flattened array elements. Happily for me, plain old vanilla JavaScript also has this ability built into the language, choosing t ...
If you have a value and you suspect that it might be contained within your array, can you somehow find the key linked to that value if it does exist? The answer is yes, thanks to the handy array_search function which searches an array for a given value and returns the corresponding key if the search is successfu ...
If you develop websites and work with JavaScript, but have never heard of jQuery before, then it is probably best that you start reading up on it right now. After all, as they like to put it themselves, jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animati ...
If you develop websites and work with JavaScript, but have never heard of jQuery before, then it is probably best that you start reading up on it right now. After all, as they like to put it themselves, jQuery is a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animati ...
Considering I work with PHP day in and day out, it is no wonder that I completely forgot that JavaScript also comes up with its super handy substring function, for when you need to grab only a specific part of a string (much like PHP's substr function). string.substring(from, to) The JavaScript substring() me ...
Craig Lotter is an established web developer and application programmer, with strong creative urges (which keep bursting out at the most inopportune moments) and a seemingly insatiable need to love all things animated. Living in the beautiful coastal town of Gordon's Bay in South Africa, he games, develops, takes in animated fare, trains under whichever martial arts dojo is closest at the time, and for the most part, simply enjoys life with his amazing wife and daughter.
Oh, and he draws ever now and then too.
This is a collection of things that he has managed to find the time to scribble down since 2007.
Looking for Something?
Jump to Category: