Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Second Split() 1

Status
Not open for further replies.

Splittle

ISP
Joined
Jun 21, 2005
Messages
26
Location
GB
Hey,

i'm looking to split every second char( ¬ )

Code:
var s="So ¬this is an ¬ example of the ¬ string to ¬ split";

making it return:

split 1 ="example of the ¬ string to"
split 2 =" split"


Thanks.
 
There isn't a built-in function for that. You can easily do it like this.
[tt]
var s="So ¬this is an ¬ example of the ¬ string to ¬ split";
var sig="¬"; //signature
var seg=2; //segment length
var sep; //separator-delimitor

var a=s.split(sig);
var aa=new Array(); //holding resultant array

for (var i=0;i<Math.floor(a.length/seg);i++) {
sep="";
aa="";
for (var j=0;j<seg;j++) {
aa+=sep+a[i*seg+j];
sep=sig;
}
}
if (a.length%seg) {
sep="";
var idx=aa.length;
aa[idx]="";
for (var i=0;i<a.length%seg;i++) {
aa[idx]+=sep+a[Math.floor(a.length/seg)*seg+i];
sep=sig;
}
}
//can return aa if set up as a function
[/tt]
It is in a form readily made into a function and sufficiently general.
 
you're too good at this, thanks, and take a star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top