How to check if the first four characters of a string match 'www.'?

10

What is the best way to verify this? More simply and with better performance.

I could only imagine doing so:

var string = "www.google.com";

if(string[0] == "w" && string[1] == "w" && string[2] == "w" && string[3] == "."){
   // string começa com www.
}
    
asked by anonymous 09.01.2015 / 14:47

4 answers

17

The code looks better (more readable) if you look at the 4 characters at a time:

if(string.substr(0, 4) === "www.") {

}

Or

if(string.substring(0, 4) === "www.") {

}

The two methods do basically the same thing, however the first one receives the initial index and the length, while the second receives the initial index and the final index (not inclusive).

Another method, which receives two indexes and extracts even before the final index:

if(string.slice(0, 4) === "www.") {

}

In terms of performance, all 3 methods seem equivalent (in V8 / Chrome).

In addition to these three, there is also the regular expression test, such as suggested by QMechanic73 , and indexOf , suggested by Sergio . It is still possible to take other paths, such as comparing each character and obtaining them via String.prototype.charAt .

Comparing with this type of solution, your original test, which checks for each individual character, is much faster

a> than any other (in my browser). That surprised me. By quickly comparing the extraction methods in the language specification, I saw no obvious reason for this. Probably the culprit for the delay of other methods is the creation of an wrapper object of type String , and later method call on that object (including verification of String of Prototypes ).

    
09.01.2015 / 14:50
9

Another alternative is the RegExp.test method.

var endereco = "www.google.com.br";

if (/^www\./.test(endereco))
    console.log("Começa com www.");
else
    console.log("Não começa com www.");

The expression ^www\. will match the www. characters at the beginning of string . About performance, the code posted in the question seems to be the fastest, as can be seen in that comparison created by @bfavaretto .

    
09.01.2015 / 15:49
6

Another variant not yet referenced to check if the string begins with these 4 characters:

if (string.indexOf("www.") == 0) // caso sim
else // caso não

.indexOf() checks the position of "www." in the string variable. If the result of .indexOf() , is 0 then the string starts with "www." .

    
09.01.2015 / 17:00
3

What QMechanic73 said is correct, even though your code looks a bit longer and faster than the others, just a simple hint to improve performance a bit, use === instead of == .

See the difference in: link

The performance varies by browser , in firefox == was faster than === and in google chrome it was% with%. The difference is that === does type conversion. For example:

1 == '1': true
1 === '1': false

How do you use string comparing to string do not need to convert type would be:

'w' === 'w': true
    
09.01.2015 / 16:42