Adding A Splice() Method To The Javascript String Prototype

Posted March 29, 2011 at 10:53 AM by Ben Nadel

Tags: Javascript / DHTML

In my blog post yesterday on using slice(), substring(), and substr() in Javascript to perform substring extraction, Andy Matthews and I got into a brief discussion about how in many languages, at a core level, strings are nothing more than arrays of characters. This concept got me thinking - if both Arrays and Strings in Javascript have a slice() method, then why don't they both have a splice() method? For fun, I wanted to see how easy it would be to modify the String prototype, adding our own splice() method.

Javascript already supports splice() on arrays. So, rather than re-inventing the wheel for strings, I figured the smartest move would be to convert our target string into an array and then leverage the already-powerful array-based splicing. Luckily, converting a string to-and-from an array can be easily accomplished with split() and join() respectively:

  • String.split( "" ) - String to character array.
  • Array.join( "" ) - Character array to string.

Ok, let's take a look at some code. In the following demo, I am updating the core String prototype. That means that our splice() method will be available to all string instances, now and in the future (since they all extend the same String.prototype object):

  • <!DOCTYPE html>
  • <html>
  • <head>
  • <title>Creating A String.splice() Method In Javascript</title>
  •  
  • <script type="text/javascript">
  •  
  •  
  • // Extend the String prototype to include a splice method.
  • // This will use an Array-based splitting / joining approach
  • // internally.
  • String.prototype.splice = function(
  • index,
  • howManyToDelete,
  • stringToInsert /* [, ... N-1, N] */
  • ){
  •  
  • // Create a character array out of the current string
  • // by splitting it. In the context of this prototype
  • // method, THIS refers to the current string value
  • // being spliced.
  • var characterArray = this.split( "" );
  •  
  • // Now, let's splice the given strings (stringToInsert)
  • // into this character array. It won't matter that we
  • // are mix-n-matching character data and string data as
  • // it will utlimately be joined back into one value.
  • //
  • // NOTE: Because splice() mutates the actual array (and
  • // returns the removed values), we need to apply it to
  • // an existing array to which we have an existing
  • // reference.
  • Array.prototype.splice.apply(
  • characterArray,
  • arguments
  • );
  •  
  • // To return the new string, join the character array
  • // back into a single string value.
  • return(
  • characterArray.join( "" )
  • );
  •  
  • };
  •  
  •  
  • // -------------------------------------------------- //
  • // -------------------------------------------------- //
  • // -------------------------------------------------- //
  • // -------------------------------------------------- //
  •  
  •  
  • // Create our test string value.
  • var message = "Katie is sort of cool.";
  •  
  • // Set the part of the string that we want to delete.
  • var lameStuff = "sort of cool";
  •  
  • // Now, let's create a more endearing message.
  • var betterMessage = message.splice(
  • message.indexOf( lameStuff ),
  • lameStuff.length,
  • "crazy-insane kinds of hot"
  • );
  •  
  • // Output the new message.
  • console.log(
  • "New message:",
  • betterMessage
  • );
  •  
  •  
  • </script>
  • </head>
  • <body>
  • <!-- Intentionally left blank. -->
  • </body>
  • </html>

As you can see, the workflow is actually quite simple:

  1. Convert string to character array.
  2. Perform array-based splicing.
  3. Convert character array back to string.

When we run the above code, we get the following console output:

New message: Katie is crazy-insane kinds of hot.

As you can see, the existing substring "sort of cool" was deleted and the new substring "crazy-insane kinds of hot" was inserted. While this operation is being executed, our base data structure becomes an array of character values. And yet, the value we are inserting is a string; this mixing of "data types" doesn't matter because when we join() the array of characters back together, it all gets flattened into one string value.

NOTE: I use "data types" here in the losest sense. Javascript doesn't actually differentiate between strings and characters (a character is a sting of length one).

While this was fun to do, I can't think of a great use-case for a string-based splice() method. Typically, when I am replacing one string into (or out of) another string, it's through the use of Javascript's regular express replace method. Even so, this exploration definitely brings to light some of the power that is affording through the use of Prototypal inheritance mechanisms.




Reader Comments

Mar 29, 2011 at 1:59 PM // reply »
2 Comments

nice article ben! any time you're augmenting the built-ins, it's a good idea to at least check for the presence of the property before adding it, something like:

  • if ( !("splice" in String.prototype) ) {
  • String.prototype.splice = function() {
  • // implementation of new method
  • }
  • }

or you can always try to default to the native's existing property (if you're polyfilling, for example with Array.forEach or Object.keys),thereby only creating it if necessary

String.prototype.splice = String.prototype.splice || function() {
// implementation of new method
}

as always, keep up the good work!


Mar 29, 2011 at 10:04 PM // reply »
10,743 Comments

@Keegan,

Ah, good call! And, while I know that Javascript is mostly consistent, there are some cross-browser inconsistency for some of the newer syntax items. In such a case, especially, you have to be checking to see if a method already exists.

Excellent catch!


Apr 3, 2011 at 1:03 PM // reply »
260 Comments

@Ben, totally tangential: Just watched the movie Splice. Similar topic to an otherwise-stale genre (The Host, Mimic, Species, etc), but with a critter that can be as lovable as Gizmo.


Apr 3, 2011 at 1:06 PM // reply »
10,743 Comments

@WebManWalking,

I've seen Splice. I actually really liked it. I have to admit, it was kind of a "hot" movie - though the ending was a bit strange. My friend wanted to take her kids to see it; I advised against it :)


Apr 3, 2011 at 2:21 PM // reply »
260 Comments

If you mean what I think you mean about "a bit strange", that was foreshadowed twice. Not that foreshadowing made it any less strange. Without giving away any spoilers, I think they must've started with the some of same genetic material as Ginger.

Anyway, it's currently showing on Cinemax if your friend wants to watch it without her kids. Here are some trailers:

http://www.splicethefilm.com/dvd/


Apr 3, 2011 at 2:22 PM // reply »
260 Comments

Oh, and I liked it to. Inventive.


Apr 3, 2011 at 6:20 PM // reply »
10,743 Comments

@WebManWalking,

Yeah, it was definitely foreshadowed with those slug creatures (or whatever the heck they were).


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2012 at 1:58 AM
Updated: Converting A ColdFusion Query To CSV Using QueryToCSV()
Hi Ben, why do you need to have so many double quotes when adding the field and field name to the row data? ----------------------------------------- <cfset LOCAL.RowData[ LOCAL.ColumnIndex ] = ... read »
AXL
May 21, 2012 at 1:24 AM
URL Rewriting And ColdFusion's WriteToBrowser Image Functionality (CFFileServlet)
@Mounir, Open your lower case URL Rewrite rule and add the following condition. Condition input: {REQUEST_URI} Check if input string: Does Not Match the Pattern Pattern: ^/CFFileServlet/_cf_ca ... read »
May 20, 2012 at 4:28 AM
Understanding The Complex And Circular Relationships Between Objects In JavaScript
@Will Vaughn I tried your javascript example but got this error:- foo.print is not a function ... read »
May 19, 2012 at 5:37 AM
A Graphical Explanation Of Javascript Closures In A jQuery Context
Thanks for this article, but I fear you missed an important point. If variables in the outer context change, these changes affect the inner anonymous functions as well. That means: if you change the ... read »
May 18, 2012 at 3:39 PM
Parsing CSV Data With An Input Stream And A Finite State Machine
Can you use file upload button with this? and read live? or does the file have to already be on the server saved? ... read »
May 18, 2012 at 1:06 AM
VIRGO (Aug. 23-Sept. 22): Dead On The Money!
A friend of mine and I were arguing about astrology and she told me that he believes in astrology. She hasn't provided me with any evidence that the belief makes any sense to me. She she been telling ... read »
May 17, 2012 at 11:32 PM
Using ColdFusion to Handle 404 Errors (Page Not Found) On Development Server
Very easy the configuration. I read a lot pages and I can't find the solution. I open the administrator and change this Administrator/server settings/Error Handlers/Missing Template Handler and p ... read »
May 17, 2012 at 3:13 PM
LOCAL Variables Scope Conflicts With ColdFusion Query of Queries
I never cease to be amazed that almost EVERY random CF issue I come across lands me on your site. Thank you for documenting your findings for the world. ... read »