SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Addict chartahir's Avatar
    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

  2. #2
    SitePoint Wizard
    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.

  3. #3
    SitePoint Enthusiast
    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.

  4. #4
    SitePoint Guru
    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.

  5. #5
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the result retured is a string
    Your question is like asking why the php function explode() that you said you wanted to mimic returns the 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);
    Try this js and see if it helps you draw any conclusions about what split() returns:
    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.
    What is your definition of an array? Is it an entity to which you can do this:

    alert(pieces[0]);
    Last edited by 7stud; Jan 10, 2007 at 01:09.

  6. #6
    SitePoint Addict chartahir's Avatar
    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 three
    chartahir

  7. #7
    SitePoint Wizard
    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(';');
    
    .....

  8. #8
    SitePoint Addict
    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

Posting Permissions

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