SitePoint Sponsor |
|
User Tag List
Results 1 to 8 of 8
Thread: JS split() function
-
Jan 9, 2007, 22:08 #1
- Join Date
- Oct 2006
- Location
- Karachi, Pakistan
- Posts
- 253
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
JS split() function
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
-
Jan 9, 2007, 22:22 #2
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You should always proofread before posting. Your post makes no sense.
-
Jan 9, 2007, 23:15 #3
- Join Date
- Sep 2005
- Posts
- 50
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Jan 9, 2007, 23:28 #4
- Join Date
- Apr 2006
- Posts
- 802
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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.
-
Jan 9, 2007, 23:56 #5
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
the result retured is a string
[0] => one [1] => two [2] => three
instead of an array when you do this:
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);
i want the reslt in an array.
alert(pieces[0]);Last edited by 7stud; Jan 10, 2007 at 01:09.
-
Jan 10, 2007, 03:55 #6
- Join Date
- Oct 2006
- Location
- Karachi, Pakistan
- Posts
- 253
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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 threechartahir
-
Jan 10, 2007, 04:07 #7
- Join Date
- Mar 2001
- Posts
- 3,537
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Code:var str = "one;two;three"; var splt = str.split(';'); .....
-
Jan 10, 2007, 04:38 #8
- Join Date
- Aug 2006
- Posts
- 266
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
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