SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to turn a single word in the input field into an array?

    Hi,

    I know that we can split() a string into an array, if we have a string in an input field like this, "javascript rocks"

    what if there is only one keyword in the input field?

    for instance, it is only a word - "javascript"?

    thanks,
    Lau

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can still split it. If the delimiter is not found, the result is an array with a single element.

    But, if this is something that you simply would never actually want to split by some delimiter, just do var arr = [str];

  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You can still split it. If the delimiter is not found, the result is an array with a single element.

    But, if this is something that you simply would never actually want to split by some delimiter, just do var arr = [str];
    got it! thank you very much


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
  •