Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Dividing a String into Sets

    I was wondering if someone could create a code to divide strings into array elements based on a parameter of the amount of characters in each element? (I've tried many different ways, but none are successful.) For instance, if I had a string with the value "foobar" and I wanted to divide it into sets of three characters in an array, I would get a result of myArray[0] = "foo" and myArray[1] = "bar".

    Any help is greatly appreciated! Thanks!
    Last edited by nolachrymose; 10-28-2003 at 11:19 PM.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Code:
    function fnStringSplit(s){
        var
             i,
            a=s.split(/(.{0,3})/);
        i=a.length;
        while(i-->0)
            if(a[i]=='')
                a.splice(i,1);
        return a;
    }
    
    
    fnStringSplit('foobar'); // => foo,bar
    fnStringSplit('1foobar'); // => 1fo,oba,r
    fnStringSplit('12foobar'); // => 12f,oob,ar
    fnStringSplit('123foobar'); // => 123,foo,bar
    That's given the browser supports full regex and Array.splice. Array.splice can be repaired, but not the regex. If you want it to search from the end of the string, that's far more complicated. The easiest thing to do is to split the string into an array of single characters, reverse it, join it, execute the regex, and split/reverse/join all the resulting parts afterward.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Hmm, if you want to supply the number of characters as a parameter you must use the RegExp constructor instead of the literal I used.

    /(.{0,3})/ --> new RegExp('(.{0,'+n+'})')
    Last edited by liorean; 10-29-2003 at 12:18 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    Regular Coder COBOLdinosaur's Avatar
    Join Date
    Jul 2002
    Location
    Canada
    Posts
    411
    Thanks
    2
    Thanked 32 Times in 32 Posts
    str='foobar';
    setsize=3;
    arr= new Array();
    for (i=0;i<str.length;i+=3)
    {
    j=arr.length;
    arr[j]=str.substring(i,i+setsize);
    }
    100% standards compliant code is 100% correct 100% of the time.
    one of my toys from my repository and perhaps some help getting help

    Cd&

  • #5
    Senior Coder
    Join Date
    Jun 2002
    Location
    frankfurt, german banana republic
    Posts
    1,848
    Thanks
    0
    Thanked 0 Times in 0 Posts
    For the sake of completeness, you can also use a global match() on the string:

    Code:
    String.prototype.makeSet = function(num) {
    	return this.match(new RegExp('.{1,' + num + '}', 'g')) || new Array();
    }
    Hmh, not really a winner in readability. The "|| new Array()" is necessary to return an array if no matches were found, as it would be the case for an empty string, which would return null.
    De gustibus non est disputandum.

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you all! I got it going now.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •