Posts Tagged: string

More fun with strings: dojo.string.contains()

In the series “convenience wrappers for small tasks that increase code readability”, today contains() is starring. Having a contains() method could also serve another purpose: to maybe prevent people from using match() to find out if a string contains a given substring (what is still proposed in some JS tutorials out there…). So, I want the contains() method to also have a switch to work case-insensitive.

Besides indexOf(), there are some other ways to achieve this, so – let’s have a competition and find out who’s the fastest!
read more »

dojo.string.beginsWith()

Most cases where you find String.substr() in the wild are to check if a given string begins with a certain other string. Be it checking for a prefix or sorting out zipcodes that begin with certain numbers. And because code readability is a good thing (really, it is important), it would be nice to have a String.beginsWith() method. Or, because of dojo love, a dojo.string.beginsWith() method.

Consider the following code:

var nearbyZipcodes = dojo.filter(givenZipcodes,function(zipcode){
    return dojo.string.beginsWith(zipcode,'12');
});

read more »