[英]Split a string in actionscript?
How do I accomplish this in actionscript (example in c#): 如何在ActionScript中完成此操作(在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++;
}
I need to count a character in an array of strings 我需要计算一个字符串数组中的字符
Try this: 尝试这个:
for(var i:int = 0; i < arr.Length; i++)
{
if(arr[i].indexOf("d") != -1)
countD++;
}
Use indexOf rather than contains. 使用indexOf而不是包含。 It will return -1 if the character is not in the string, otherwise the string contains at least one instance.
如果字符不在字符串中,它将返回-1,否则字符串包含至少一个实例。
Use the match
function on a javascript string. 在javascript字符串上使用
match
函数。 http://www.cev.washington.edu/lc/CLWEBCLB/jst/js_string.html http://www.cev.washington.edu/lc/CLWEBCLB/jst/js_string.html
Sorry, works the same. 抱歉,工作原理相同。
Found it: 找到了:
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
Here are four ways to do it... (well, 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.