Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    42
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Check for space/whitespace in string

    Basically I have this:

    function semaphore(phrase)
    {
    var len = phrase.length;
    var str1 = "";
    for (var i=0;i<len;i++)
    {
    if(phrase[i]=" ")
    {
    phrase[i] = "interval";
    }
    var str = "images/semaphore/" + phrase[i] + ".gif";
    str1 = str1 + "<img src='" + str + "'>";
    }
    return str1;
    }

    so if phrase = i love coding forums

    I want all the spaces to be replaced with the text "interval"

    Thanks
    Danny

  • #2
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Hayes View Post

    so if phrase = i love coding forums

    I want all the spaces to be replaced with the text "interval"

    Thanks
    Danny
    Just to clarify, then the output string needs to be:

    Code:
    iintervalloveintervalcodingintervalforums

  • #3
    New Coder
    Join Date
    Nov 2007
    Posts
    42
    Thanks
    3
    Thanked 0 Times in 0 Posts
    pretty much as im taking each letter and outputting a image depending on the letter for example if the phrase is danny print d.gif, a.gif etc

    a whitespace is interval.gif

  • #4
    Banned
    Join Date
    Apr 2011
    Posts
    656
    Thanks
    14
    Thanked 69 Times in 69 Posts
    Quote Originally Posted by Hayes View Post
    pretty much as im taking each letter and outputting a image depending on the letter for example if the phrase is danny print d.gif, a.gif etc
    That's all one sentence and I can't tell for sure when one sentence starts and another ends or where you phrase starts and ends. I haven't got time to "decode" what you posted.

    Does the concept of punctuation mean anything to you?

  • #5
    New Coder
    Join Date
    Nov 2007
    Posts
    42
    Thanks
    3
    Thanked 0 Times in 0 Posts
    whats wrong with that it was one sentence?

    each letter in the phrase corresponds to a image and the image name for a space is interval.gif

  • #6
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,355
    Thanks
    11
    Thanked 558 Times in 551 Posts
    Quote Originally Posted by Hayes View Post
    I want all the spaces to be replaced with the text "interval"
    I'm sure you don't want this.

    What you want instead: Get each letter from "phrase" and append ".gif" to it. You will get "a.gif", "b.gif" etc. If the current character is a space character, then take "interval.gif". This is not what you said ... but it's what you want :-)

    Code:
    function semaphore(phrase)
    {
       var len = phrase.length;
       var str1 = "";
       var str;
       for (var i=0;i<len;i++)
       {
          if(/\s/.test(phrase[i]))
          {
             str = "images/semaphore/interval.gif";
          } else
          {
             str = "images/semaphore/" + phrase[i] + ".gif";
          }
          str1 = str1 + '<img src="' + str + '">';
       }
       return str1;
    }


  •  

    Posting Permissions

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