Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

startsWith.as


Name: startsWith()/endsWith() - checks if string starts with/ends with passed string
Author: senocular: www.senocular.com
Date: 1899-12-31T00:03:06.500
Documentation:
String STARTSWITH and ENDSWITH: Two simple functions
which determine if a string starts with or ends with
another string.

Arguments:
- str: the string to check for starting or ending the this string

Returns:
TRUE or FALSE depending on whether or not the passed string starts or ends the desired string.

* @example
text = "howdy";
trace(text.startsWith("h")); // true
trace(text.startsWith("how")); // true
trace(text.startsWith("hot")); // false
trace(text.startsWith("howdy mate")); // false

trace(text.endsWith("y")); // true
trace(text.endsWith("wdy")); // true
trace(text.endsWith("how")); // false
trace(text.endsWith("ady")); // false

1
2
3
4
5
6
7
8
9
String.prototype.startsWith = function(str){
	return !this.indexOf(str);
}

String.prototype.endsWith = function(str){
	var end = this.lastIndexOf(str);
	if (end == -1) return false
	return end == this.length-str.length;
}