string 如何确定一个字符串是否包含特定的子字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2733935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:43:36  来源:igfitidea点击:

How to determine if a string contains a specific substring

apache-flexflashactionscript-3string

提问by Rella

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

给定一个字符串A,如何确定该字符串是否包含子字符串"video/x-flv"

回答by spender

A.indexOf("video/x-flv") >= 0

回答by CodeMonkeyBran

This is a little old now, but try if(A.indexOf(video/x-flv) != -1){ //Found it }

这现在有点旧了,但试试 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!

如果子字符串没有出现在 indexOf 中,它将返回 -1。因此,如果它不是 -1,则表示它确实存在,希望这会有所帮助,尽管我可能有点晚了!

回答by Mexican Seafood

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 --

-- 或作为 1-liner --

if (/video\/x-flv/g.test(A)) {...}

RegExp.test() returns a Booleanso the test is clearer than comparing to an arbitrary value of -1 (to me at least).

RegExp.test() 返回一个布尔值,因此测试比与 -1 的任意值进行比较(至少对我而言)更清晰。

However, remember that this method is slightly slower than indexOf (source).

但是,请记住,此方法比 indexOf ( source)稍慢。

回答by Glycerine

http://www.gskinner.com/blog/archives/2007/04/free_extension.html

http://www.gskinner.com/blog/archives/2007/04/free_extension.html

gSkinner hasText() function

gSkinner hasText() 函数

EDIT:

编辑:

NO Sorry - contains()

不抱歉 - contains()

回答by Wilensky

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.

或者您可以简单地使用您需要查找的字符串,“筛选” RegExp 的所有服务字符(如果它们存在)或使用 RegExp 模式。

Good Luck!

祝你好运!

回答by invertedSpear

if(myString.indexof("A",0)>0)

if(myString.indexof("A",0)>0)

回答by firos

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);