split or explode?

Discussion in 'JavaScript' started by gilgalbiblewheel, Nov 6, 2008.

  1. #1
    How do you split a string in JavaScript?
    mystring = "my dog ate my homework";
    Code (markup):

     
    gilgalbiblewheel, Nov 6, 2008 IP
  2. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    var words = mystring.split(' ');
    Code (markup):
    returns an Array.
     
    keyaa, Nov 6, 2008 IP
  3. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #3
    How do you determine the length of a string?
     
    gilgalbiblewheel, Nov 6, 2008 IP
  4. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #4
    var stringlength = mystring.length;
    Code (markup):
    You're welcome.
     
    keyaa, Nov 6, 2008 IP
  5. gilgalbiblewheel

    gilgalbiblewheel Well-Known Member

    Messages:
    435
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #5
    I don't understand this. How do you put the string back together after you have split them? My intention is to sort out the words of 4 letters and under.
    	var mySplitString = myNewString.split(" ");
    	//if(mySplitString[i].length>4){
    		for (j = 0; j < mySplitString[j].length; j++){
    			if(j==0){
    				document.getElementById('keyword0').innerHTML += mySplitString;
    				getKeyURL+=mySplitString;
    			}else{
    				document.getElementById('keyword0').innerHTML += " " + mySplitString;
    				getKeyURL+=" "+mySplitString;
    			}
    		}
    Code (markup):
     
    gilgalbiblewheel, Nov 6, 2008 IP
  6. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Your loop makes no sense.

    var text = 'foo bar baz';
    var words = text.split(' ');
    for(var i = 0; i < words.length; i++) {
      // in this context, .length is the number of elements within an array
      // words[i] is the current word, do something with it
      if(words[i].length < 4) {
        // in this context, .length is the character count of a string
        // short word... 
      } else {
        // longer word...
        document.getElementById('keyword0').innerHTML += mySplitString + ' ';
      }
    }
    
    Code (markup):
     
    keyaa, Nov 6, 2008 IP
  7. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #7
    To join pieces of array into string:
    
    var string=array.join([ separator])//separator is optional
    
    Code (markup):
     
    rohan_shenoy, Nov 7, 2008 IP
  8. keyaa

    keyaa Peon

    Messages:
    137
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #8
    rohan_shenoy, didn't you read his request?
    He needs to filter out certain entries from his array, not join them all together.
     
    keyaa, Nov 7, 2008 IP