mikemastah Posted May 23, 2009 Share Posted May 23, 2009 I was wondering is it possible to split a variable per 3 characters like split(" "); but not split at a space but really every three characters. Quote Link to comment Share on other sites More sharing options...
Floydian Posted May 24, 2009 Share Posted May 24, 2009 Re: split variable stringVariable.slice(startIndex, endIndex); Using that, if you keep adding 3 to startIndex and endIndex, in a loop, you can achieve what you want. Hope that helps... Quote Link to comment Share on other sites More sharing options...
mikemastah Posted May 24, 2009 Author Share Posted May 24, 2009 Re: split variable looks like what I need but I need some help to use it I'm afraid. At this time I have a var 'mrnaCodes' which is the value of an input field. I used to do this "var mrnaArray = mrnaCodes.split(" ");" for the input with spaces (ex1) BLA BLA BLA BLA BLA but now I want them able to choose between ex1 and ex2. BLABLABLABLABLA I do need however the same array mrnaArray[0]="BLA" mrnaArray[2]="BLA" etc. Quote Link to comment Share on other sites More sharing options...
mikemastah Posted May 25, 2009 Author Share Posted May 25, 2009 Re: split variable ah, figured it out. /*User input*/ var mrnaCodes = "AAAUUUCCCGGGAAAUUUCCCGGGGGGCCCUUUAAA"; function someThing(); { var startSlice = 0; var mrnaArray=new Array(); while(startSlice<mrnaCodes.length) { endSlice = startSlice+3; var i=startSlice/3; mrnaArray[i] = mrnaCodes.slice(startSlice,endSlice); startSlice = startSlice+3; } } thanks for the help Quote Link to comment Share on other sites More sharing options...
Floydian Posted May 26, 2009 Share Posted May 26, 2009 Re: split variable You're welcome for the help, and glad that worked for ya. Yes, that's exactly how to do that, good job ;) One suggestion: To automatically tell javascript to add an element to the end of an array (similar to PHP's method: $array[] = $next), use this: mrnaArray[mrnaArray.length] This will only work with numeric arrays, of course. Quote Link to comment Share on other sites More sharing options...
mikemastah Posted May 28, 2009 Author Share Posted May 28, 2009 Re: split variable ok, thanks for the info. I changed it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.