Given a string A
, how can I determine if that string contains the substring "video/x-flv"
?

- 12,458
- 3
- 39
- 44

- 65,003
- 109
- 363
- 636
-
@spender - yep - sometimes I just do not have time to check them all. Sorry=) – Rella Apr 29 '10 at 00:16
-
I wonder when people will stop asking questions that can be answered by a simple look at the documentation... – bug-a-lot Apr 29 '10 at 14:15
7 Answers
A.indexOf("video/x-flv") >= 0

- 117,338
- 33
- 229
- 351
-
4Please don't jam your operators together with your operands without whitespace like this. `A.indexOf("video/x-flv") >= 0` is significantly more readable (and fits the style guidelines). – Cory Petosky Apr 29 '10 at 00:17
-
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!

- 21
- 1
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).

- 504
- 3
- 13
http://www.gskinner.com/blog/archives/2007/04/free_extension.html
gSkinner hasText() function
EDIT:
NO Sorry - contains()

- 7,157
- 4
- 39
- 65
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!

- 11
- 1
if(myString.indexof("A",0)>0)

- 10,864
- 5
- 39
- 77
-
That's quite incorrect. It might still work sometimes, but who'd want something that works only sometimes? – bug-a-lot Apr 29 '10 at 14:14
-
1the only thing I see wrong with it is that I forgot the `>=` and just did a `>`. What else is wrong about it? Would be much more effective criticism if you explained why you think it's wrong rather than just claiming that it is. Thanks – invertedSpear Apr 29 '10 at 15:30
-
@mini-me - formatting makes no difference in this situation, and example code is just as relevant as using actual values from the question, sometimes it can be even more useful as it removes noise. Now I still have no answer as to why this is incorrect, or explanation as to why it would only "work sometimes" as bug-a-lot put it. I can accept that it's wrong if it is, but it doesn't help anyone to just say "wrong" with no constructive criticism. – invertedSpear Nov 07 '13 at 23:53
-
[Compare](http://stackoverflow.com/questions/2733935/how-to-determine-if-a-string-contains-a-specific-substring/2733954#2733954) – Bitterblue Nov 08 '13 at 08:09
-
@mini-me - Still not constructive as it doesn't explain why mine would "only work sometimes" other than the already noted operator difference. As far as I can tell they are pretty much the same thing just written slightly differently. – invertedSpear Nov 08 '13 at 18:12
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);

- 1
-
You could at least point to the portion of that code that is pertinent to the question. – Mexican Seafood Aug 23 '12 at 21:22