在actionscript中拆分一个字符串?

时间:2009-04-20 14:40:03

标签: string actionscript actionscript-2

如何在动作脚本中完成此操作(c#中的示例):

string[] arr = { "1.a", "2.b", "3.d", "4.d", "5.d" };
int countD = 0;

for (int i = 0; i < arr.Length; i++)
{
    if (arr[i].Contains("d")) countD++;
}

我需要计算字符串数组中的字符

4 个答案:

答案 0 :(得分:2)

试试这个:

for(var i:int = 0; i < arr.Length; i++)
{
    if(arr[i].indexOf("d") != -1)
        countD++;
}

使用indexOf而不是contains。如果字符不在字符串中,它将返回-1,否则该字符串至少包含一个实例。

答案 1 :(得分:0)

在javascript字符串上使用match函数。 http://www.cev.washington.edu/lc/CLWEBCLB/jst/js_string.html

抱歉,也是这样。

答案 2 :(得分:0)

找到它:

var searchString:String = "Lorem ipsum dolor sit amet.";
var index:Number;

index = searchString.indexOf("L");
trace(index); // output: 0

index = searchString.indexOf("l");
trace(index); // output: 14

index = searchString.indexOf("i");
trace(index); // output: 6

index = searchString.indexOf("ipsum");
trace(index); // output: 6

index = searchString.indexOf("i", 7);
trace(index); // output: 19

index = searchString.indexOf("z");
trace(index); // output: -1

答案 3 :(得分:0)

以下是四种方法......(好吧,3.something)

var myString:String = "The quick brown fox jumped over the lazy "
           + "dog. The quick brown fox jumped over the lazy dog.";
var numOfD:int = 0;

// 1# with an array.filter
numOfD = myString.split("").filter(
            function(s:String, i:int, a:Array):Boolean {
                return s.toLowerCase() == "d"
            }
        ).length;

trace("1# counts ", numOfD); // output 1# counts 4


// 2# with regex match
numOfD = myString.match(/d/gmi).length;
trace("2# counts ", numOfD); // output 2# counts 4

// 3# with for loop
numOfD = 0;
for (var i:int = 0; i < myString.length; )
    numOfD += (myString.charAt(++i).toLocaleLowerCase() == "d");
trace("3# counts ", numOfD); // output 3# counts 4

// 4# with a new prototype function (and regex)
String['prototype'].countOf = 
    function(char:String):int { 
        return this.match(new RegExp(char, "gmi")).length;
    };
// -- compiler 'strict mode' = true
numOfD = myString['countOf']("d");
trace("4# counts ", numOfD); // output 4# counts 4
// -- compiler 'strict mode' = false
numOfD = myString.countOf("d");
trace("4# counts ", numOfD); // output 4# counts 4