How to determine if a string contains a specific substring

╄→гoц情女王★ 提交于 2020-01-03 07:21:08

问题


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,"'","&quote;");
trace(str);

str = findAndReplace(str,"&quote;","'");
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 = "&quote; I hate &quote; when content has ' words like, ' in i"
            theContent = theContent.split("&quote;").join("'");
            trace(theContent);


来源:https://stackoverflow.com/questions/2733935/how-to-determine-if-a-string-contains-a-specific-substring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!