5306 posts Interactive Design
  • Elite Author
  • Author Level 10
  • Trendsetter
  • Top Monthly Author
+11 more
digitalscience
says

How do you check if a string contains http:// or not? Thanks :)

3256 posts
  • Affiliate Level 4
  • Author Level 9
  • Collector Level 2
  • Top Monthly Author
+11 more
ParkerAndKent
says
How do you check if a string contains http:// or not? Thanks :)
if (myString.split("http://").length > 1) { // is an url... }

:-)

6231 posts
  • Exclusive Author
  • Elite Author
  • Author Level 8
  • 7 Years of Membership
+13 more
VF
says

Or use indexOf() to get same result:

if (myString.indexOf("http://")>-1) {
    trace("This string has a link");
}
3256 posts
  • Affiliate Level 4
  • Author Level 9
  • Collector Level 2
  • Top Monthly Author
+11 more
ParkerAndKent
says

And to be sure that the string starts with “http://”


if (myLink.split("http://")[0] == "http://") {}

or

if (myLink.indexOf("http://") == 0) {}

:-)

5306 posts Interactive Design
  • Elite Author
  • Author Level 10
  • Trendsetter
  • Top Monthly Author
+11 more
digitalscience
says

ok thanks!

so what is length > 1… the length of the remaining bit where it split the string at “http://” ?

3256 posts
  • Affiliate Level 4
  • Author Level 9
  • Collector Level 2
  • Top Monthly Author
+11 more
ParkerAndKent
says
ok thanks! so what is length > 1… the length of the remaining bit where it split “http://” ?
The split function returns an array, so if you have:

myString = "a,b,c,d,e";

if you do 

myString.split(",") you will get an array of 5 elements, so the length will be 5

if you do

myString.split("hello") the length of the array will be 0 because the separator won't be found
<pre /></pre>
3256 posts
  • Affiliate Level 4
  • Author Level 9
  • Collector Level 2
  • Top Monthly Author
+11 more
ParkerAndKent
says

So basically if the url is valid you will get with the split an array with 2 elements.

At the index 0 you will have the “http://” and at the index 1 you will have the other part of the url.

891 posts Thinking Big
  • Trendsetter
  • Weekly Top Seller
  • Elite Author
  • Author Level 7
+8 more
MoeHaydar
says

Intresting, I used to use VF’s method IndexOf but ParkerAndKent method is more advanced thanks

3256 posts
  • Affiliate Level 4
  • Author Level 9
  • Collector Level 2
  • Top Monthly Author
+11 more
ParkerAndKent
says
Intresting, I used to use VF’s method IndexOf but ParkerAndKent method is more advanced thanks

Thanks, but it isn’t more advanced… we just use two functions that have different purposes but that give the same result in a case like this one ;-)

940 posts
  • Affiliate Level 3
  • Author Level 5
  • Collector Level 2
  • Top Monthly Author
+5 more
michelsteege
says

I also use indexOf split creates a array and you don’t need a array for this :)

by
by
by
by
by
by