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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts

    Split a string by offset(s) instead of character

    Code:
    String.prototype.splitByOffset=function() {
    var offset=Array.prototype.slice.call(arguments);
    offset.sort(function(a,b) {return a-b});
    offset.unshift(0);
    offset.push(this.length);
    var temp=[];
    for(var i=0;i<offset.length-1;i++) {
    var s=Math.round(offset[i]);
    var e=Math.round(offset[i+1]);
    temp[i]=this.substring(s,e);
    }
    return temp;
    }
    The arguments this takes are offsets e.g

    Code:
    var str="onetwothree";
    
    alert(str.splitByOffset(6,3).join(" "))
    alerts 'one two three'

    This one just takes one argument (n) and splits a string into substrings of that length:

    Code:
    String.prototype.splitEvery=function(n) {
    n=Math.round(n);
    var pos=0;
    var temp=[];
    for(i=0;i<Math.ceil(this.length/n);i++) {
    temp[i]=this.substr(pos,n);
    pos+=n;
    }
    return temp;
    }
    eg.

    Code:
    alert(str.splitEvery(2).join("-"));
    alerts 'on-et-wo-th-re-e'
    Last edited by gusblake; 09-21-2009 at 10:48 PM. Reason: addition of Math.round to prevent non-integer arguments

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,375
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by gusblake View Post
    This one just takes one argument (n) and splits a string into substrings of that length:
    Code:
    String.prototype.splitEvery=function(n) {
    n=Math.round(n);
    var pos=0;
    var temp=[];
    for(i=0;i<Math.ceil(this.length/n);i++) {
    temp[i]=this.substr(pos,n);
    pos+=n;
    }
    return temp;
    }
    here's a simpler/faster version: (you might not even need a prototype it's so short)

    Code:
    String.prototype.splitEvery=function(n) {
      return this.match(new RegExp(".{"+n+"}","g"));
    }
    results are the same as the second example's.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #3
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    Except you lose (str.length%n) characters from the end.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,375
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by gusblake View Post
    Except you lose (str.length%n) characters from the end.
    tochè.

    Code:
    String.prototype.splitEvery=function(n) {
      return this.match(new RegExp(".{1,"+n+"}","g"));
    }
    i stand corrected.
    Last edited by rnd me; 09-22-2009 at 12:43 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Tags for this Thread

    Posting Permissions

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