问题
Given a string A, how can I determine if that string contains the substring "video/x-flv"?
回答1:
A.indexOf("video/x-flv") >= 0
回答2:
This is a little old now, but try
if(A.indexOf(video/x-flv) != -1){ //Found it }
indexOf will return -1 if the substring doesn't appear in it. So if it's anything but -1 it means it does exist, Hope this helps although I'm probably a bit late!
回答3:
http://www.gskinner.com/blog/archives/2007/04/free_extension.html
gSkinner hasText() function
EDIT:
NO Sorry - contains()
回答4:
if (someString.search('\\[someValueInSquareBracketsForExmaple\\]') == -1) Alert.show('String not found!')
else Alert.show('String found!')
Or you can just simply use the string you need to find, 'screening' all service characters of RegExp if they exist or use RegExp pattern.
Good Luck!
回答5:
Just to add variety, a solution using regular a expression:
var videoTypeMatcher:RegExp = /video\/x-flv/g;
if (videoTypeMatcher.test(A)) {...}
-- or as a 1-liner --
if (/video\/x-flv/g.test(A)) {...}
RegExp.test() returns a Boolean so the test is clearer than comparing to an arbitrary value of -1 (to me at least).
However, remember that this method is slightly slower than indexOf (source).
回答6:
if(myString.indexof("A",0)>0)
回答7:
here is a function to replace single quote with a string..
var str:String = "hello'welcome'";
str = findAndReplace(str,"'",""e;");
trace(str);
str = findAndReplace(str,""e;","'");
trace(str);
function findAndReplace(haystack:String, needle:String, replace:String)
{
while(haystack.indexOf(needle)>=0) {
haystack = haystack.replace(needle,replace);
}
return haystack;
}
Another easy method is
var theContent:String = ""e; I hate "e; when content has ' words like, ' in i"
theContent = theContent.split(""e;").join("'");
trace(theContent);
来源:https://stackoverflow.com/questions/2733935/how-to-determine-if-a-string-contains-a-specific-substring