I was working on a project recently where I recieved a data feed and needed to split it into sections that would fit into a text box (ie. 200 characters at a time). The user would then click ‘more‘ and progress through the text in segments until they had read all of it.

I wrote this simple prototype, which splits a string into sections of a defined size (and optionally adds a postfix, ie. “…” to the end), and returns an array containing the segmented text.

Here is the prototype:

String.prototype.splitUp = function (maxLetters, postFix)
	var txt:String = this + 1;
	var split:Array = new Array ();

	var num = 0;
	var counter = 0;
	while (num >= 0)
		var num = txt.indexOf (" ", maxLetters);

		if (postFix != undefined && num >= 0)
			var t = txt.slice (0, num) + postFix;
			var t = txt.slice (0, num);

		split[counter] = t;
		txt = txt.slice (num + 1, txt.length);

	return split;

… And an example of it’s usage – in this instance the string is converted to an array which can then be used to populate a text box:

var phrases = blurb.splitUp (190, "...");

It may have some bugs as I have only tested it within the context of the project I wrote it for. With a little added Actionscript though, it should be a useful tool.

Posted on 08 Feb 2007
Posted in
0 Trackbacks


Splitting Strings was posted on February 8th 2007 in the category Code / Code and tagged; , , .

You can Leave a comment.

Warning: file_get_contents(http://search.twitter.com/search.atom?q=from:soulwire&rpp=1) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 410 Gone in /home/soulwire/webapps/soulwire_blog/wp-content/themes/soulwire/functions.php on line 203


6 Responses to Splitting Strings

Leave a Reply

  1. Tim 10 years ago

    Your String segmentation was a lifesaver…THANK YOU!

    Reply to this comment

  2. kristian 8 years ago

    Hey, I like the new layout!

    I would like to use this splitter, but i’m new to prototypes. when i added this code, i then did what you said, and wrote:

    var bio_split = bio.splitUp(500, "...");
    textBox.text = bio_split;

    when i wrote that, it gave me an error saying that there is no splitUp method, which obviously there wouldn’t be, in flash’s eyes. How do i register it? or is it simpler than that?


    Reply to this comment

  3. Soulwire 8 years ago

    Hi Kristian,

    The prototype should be registered to the string class automatically when the movie is compiled. is ‘bio’ a variable which you declare as a string earlier in your code? When you get the error message, does it tell you which class does not contain the method splitUp? i.e. does the compiler tell you there is no method splitUp in class String, or something else?

    Reply to this comment

  4. kristian 8 years ago

    Thanks for the response,

    The Error goes like this:

    **Error** Scene=Scene 1, layer=strings, frame=1:Line 32: There is no method with the name 'splitUp'.
         var bio_split = bio.splitUp(190, "...");
    Total ActionScript Errors: 1 	 Reported Errors: 1
    frame one

    and i’ve used all the code in this order:
    1. splitter proto code.
    2. my string
    3. the call to split my string.

    Reply to this comment

  5. Soulwire 8 years ago

    Try not declaring your variable ‘bio; as a string (this is why I asked in my last comment). By not declaring its datatype it will force the compiler to set this itself, and so will make it a string (with the new methods in the prototype) when it compiles. I can’t claim to fully understand the reasons behind this, perhaps the compiler does things in an unexpected order. Extending MovieClips is never a problem; then again, String is a more intrinsic datatype.

    Reply to this comment

  6. Saw your blog bookmarked on Digg.I love your site and marketing strategy.

    Reply to this comment