...

View Full Version : Check for space/whitespace in string



Hayes
03-20-2012, 12:20 PM
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

webdev1958
03-20-2012, 12:24 PM
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: :confused:


iintervalloveintervalcodingintervalforums

Hayes
03-20-2012, 12:33 PM
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

webdev1958
03-20-2012, 12:39 PM
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? :cool:

Hayes
03-20-2012, 12:40 PM
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

devnull69
03-20-2012, 12:54 PM
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 :-)



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;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum