I'm using the js split() function,
orignal string => "+;5;5"
the result retured is a string , that is => "+,5,5"
i want the reslt in an array.
???
| SitePoint Sponsor |


I'm using the js split() function,
orignal string => "+;5;5"
the result retured is a string , that is => "+,5,5"
i want the reslt in an array.
???
Last edited by chartahir; Jan 9, 2007 at 23:15.
chartahir





You should always proofread before posting. Your post makes no sense.
It is creating an array. I'm assuming you are using alert() or document.write() to see the value, which uses the Array().toString method which joins it back with a comma for ease of display. Try alerting the length (array.length) for proof.




If you call somestring.split() with no arguments the original string is returned,
otherwise it returns an array of substrings. somestring.split(',') or somestring.split(/\s*,\s*/)
If you call split with a delimeter that is not in the string, a one element array is returned.
If you call split('') with the empty string ,the returned value is the indexed array of each character in the string .
If you want to include the delimeters in the array, split on a parenthesized regular expression=
somestring.split(/(,)/);
Last edited by mrhoo; Jan 10, 2007 at 00:20.





Your question is like asking why the php function explode() that you said you wanted to mimic returns the string:the result retured is a string
[0] => one [1] => two [2] => three
instead of an array when you do this:
Try this js and see if it helps you draw any conclusions about what split() returns:Code:$str = "one two three"; $arr = explode(" ", $str); print_r ($arr);
Code:var str = "one two three"; alert(typeof str); var arr = [1, 2, 3]; alert(typeof arr); var pieces = str.split(" "); alert(typeof pieces);What is your definition of an array? Is it an entity to which you can do this:i want the reslt in an array.
alert(pieces[0]);
Last edited by 7stud; Jan 10, 2007 at 01:09.


I want to access the each value by index, and i've done it, have a look at it, it might help u ppl too :
var str = "one;two;three";
var splt = new Array();
slpt = str.split(';');
Now if you want to access the three words separately you can do this by :
alert(splt[0]); // value at index 0 is one
alert(splt[1]); // value at index 1 is two
alert(splt[2]); // value at index 2 is three
chartahir





Code:var str = "one;two;three"; var splt = str.split(';'); .....


Code:<script type="text/javascript"> var str = "one;two;three"; var arr = new Array(3) ; for(i = 0; i < arr.length; i++){ arr[i] = str.split(';')[i] ; alert(arr[i]) ; } alert(arr) ; </script>Code:<script type="text/javascript"> var str = "one;two;three"; a = str.split(';')[0] ; alert(a) ; b = str.split(';')[1] ; alert(b) ; c = str.split(';')[2] ; alert(c) ; </script>Code:<script type="text/javascript"> var str = "one;two;three"; var splt = str.replace(/;/g,","); alert(splt) ; </script>Code:<script type="text/javascript"> var str = "one;two;three"; var newStr=""; for(i = 0; i < str.length; i++){ if(str.charAt(i) != ";") { newStr+=str.charAt(i) ; } if(str.charAt(i) == ";") { newStr+="," ; } } alert(newStr); </script>
Last edited by muazzez; Feb 21, 2007 at 15:48.
Bookmarks