Results 1 to 8 of 8

Thread: [RESOLVED] Javascript split on only first space

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jul 2009
    Posts
    71

    Resolved [RESOLVED] Javascript split on only first space

    I guess this is a stupid questen but I cannot find a solution. What I want is to split a string, but with a limit. Say I have the following string:

    20 nice cookies

    I'd like to get an array containing:

    20
    nice cookies

    Splitting with a space returns 3 elements, and with a limit of 1 it returns 1 element (20), and the rest is discarded. How do I accomplish what I want?

    Thanks.

  2. #2

    Thread Starter
    Registered User
    Join Date
    Jul 2009
    Posts
    71

    Re: Javascript split on only first space

    Well, I decided to make my own function for this, it works at least.

    parseOut('I have 20 nice cookies', ' ', 2, ' ', 1);

    returns 20. It's not exactly what I was asking for but this is what I needed.
    str - the string
    before - the first delimiter
    index1 - where the first delimiter is (in the above example, the space between I and have should be ignored therefore index 2)
    after - the second delimiter
    index2 - where the second delimiter is, counting from the first delimiter

    indices are 1-based, not zero based (for my own pleasure ).

    Code:
    function parseOut(str, before, index1, after, index2) {
     var c = 0;
     var c2 = 0;
     var res = '';
     if(index1 > -1) {
      for(var i = 0; i < index1; i++) {
       c=str.indexOf(before, c + 1);
      }
      res = str.slice(c + before.length);
     }
     else {
      res = str;
     }
     if(index2>-1) {
      for(i = 0; i < index2; i++) {
       c2 = res.indexOf(after, c2 + 1);
      }
      res = str.slice(c + before.length, c + c2 + before.length);
     }
     else {
      res = str.slice(c + before.length);
     }
     return res;
    }
    Last edited by pimvdb; Aug 24th, 2010 at 09:09 AM.

  3. #3
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] Javascript split on only first space

    use the split() method:

    Code:
    var string = "20 cookies";
    
    alert(string.split(" ", 1)[0]);

  4. #4

    Thread Starter
    Registered User
    Join Date
    Jul 2009
    Posts
    71

    Re: [RESOLVED] Javascript split on only first space

    In such case it's very simple indeed, but I needed a function so that I can also get '20' out of 'I have 20 nice cookies' or something like that. But the function I posted above works perfectly for me.

  5. #5
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] Javascript split on only first space

    why didn't you post saying that you needed "20" from that string rather than the string you had posted then? either way, if you're looking to get the third word from a sentence then it's just as simple to modify the split() call and return the third element:

    Code:
    string.split(" ")[2]
    there's no reason to reinvent the wheel. if you're looking to get the number from a specific string, then you might consider using a regular expression to capture it instead.

  6. #6

    Thread Starter
    Registered User
    Join Date
    Jul 2009
    Posts
    71

    Re: [RESOLVED] Javascript split on only first space

    Thanks for your reply and sorry for not being clear. The string I am actually working with is (it's from a game):

    Selling 1 H.a.m. boots at 1810, 0&#37; complete, earning 0 gp so far

    I should also be able to capture 'H.a.m. boots' from it, so I first have to split on the second space and then at ' at '. Because 'H.a.m. boots' also contains a space and both delimiters are not the same, I needed a more advanced version of the split method which I couldn't find.

  7. #7
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] Javascript split on only first space

    if things are always in this format:

    Code:
    Selling {quantity} {item_name} at {price}, {percentage} complete, earning {earning} gp so far
    then you can write a regular expression that lets you get at whatever you want. you're welcome to use the one I wrote:

    Code:
    // Regular expression
    var regex  = new RegExp('Selling ([0-9]+) (.+) at ([0-9]+), ([0-9]+\&#37;) complete, earning ([0-9]+) gp so far');
    
    // String to match against
    var string = "Selling 1 H.a.m. boots at 1810, 0% complete, earning 0 gp so far";
    
    // Execute regular expression
    var match = regex.exec(string);
    
    // Does it match?
    if(string.match(regex)){
      
      // Assign variables
      var quantity    = match[1]
        , item_name   = match[2]
        , price       = match[3]
        , percentage  = match[4]
        , earning     = match[5];
      
      // Dump
      alert('Quantity: '    + quantity    + "\n"
          + 'Item name: '   + item_name   + "\n"
          + 'Price: '       + price       + "\n"
          + 'Percentage: '  + percentage  + "\n"
          + 'Earning: '     + earning     + "\n");
    }

  8. #8

    Thread Starter
    Registered User
    Join Date
    Jul 2009
    Posts
    71

    Re: [RESOLVED] Javascript split on only first space

    I'm a very beginner and don't even understand the basics of regexp yet. It looks much clearer and also very useful in certain situations like this. Thanks a lot, I'll start searchiing for some regexp tutorials!

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width