How can I check that a string contains a certion word actionscript?
The indexOf method
If you want to check for the existence of a word in strict sense (as a whole word, not part of another word), you can use regular expressions. For example, the string "hat"
is contained in the string "I like that movie"
as a substring but not as whole word. Use the String::match(pattern:*)
method to search for regular expressions in a string.
var str:String = "I like that movie";
var t1:RegExp = /hat/; // `/` is the delimiter
var t2:String = /\bhat\b/; // `\b` denotes word boundary
if(str.match(t1) != null)
trace("hat is a substring");
if(str.match(t2) == null)
trace("but hat is not present as whole word");
精彩评论