Click to See Complete Forum and Search --> : string manipulation


lcscne
12-12-2003, 12:15 PM
How can I check the left most characters of a string? eg...

var linkURL = document.frmTest.atag.value
var atagString

if (left(linkURL,5) == "http:")
{
atagString = "<a href='"+linkURL+"'>";
}
else
{
atagString = "<a href='http://"+linkURL+"'>";
}


I know the left function call is invalid in js, is there a valid function that will work?

TheBearMay
12-12-2003, 12:25 PM
Look at substring, something like

if (linkURL.substring(0,5) == 'http:'.....

olerag
12-12-2003, 12:25 PM
Use substring.... such as,

var myString = "http://myWebPage";
alert(myString.substring(0,5));

xondokan
12-12-2003, 12:38 PM
function check_http(str_new) {
if (str_new.indexOf('http://')==0) {
return true
} else {
return false
}
}

lcscne
12-12-2003, 12:45 PM
you people are awsome

i love this place.

if you havn't guessed substring worked perfectly.

Thanks much

fredmv
12-12-2003, 12:49 PM
<script type="text/javascript">
//<![CDATA[
String.prototype.beginsWithHTTP = function()
{
return /^http:/i.test(this);
}

alert('http://www.foo.com/'.beginsWithHTTP()); // true
alert('ftp://www.bar.com/'.beginsWithHTTP()); // false
//]]>
</script>