AS2检查字符串是否包含特定顺序的字母

时间:2018-03-04 18:49:27

标签: flash actionscript actionscript-2

我正在尝试在Action Script 2中创建一个代码,用于检查字符串是否包含某些字符。 例如: 字符串1 - “wqeeqwe j qwqwq a retrt v iyiiyi a ”; 很明显,这里隐藏的词是 java 。我正在尝试制作一个类似的代码: 如果字符串按顺序包含字母“j,a,v,a”,那么------

任何帮助?

1 个答案:

答案 0 :(得分:1)

不确定AS2语法,但这样的事情应该有效:

function containsWord(originalString:String, word:String):Boolean
{
    var lettersFound:Number = 0;

    for(i=0; i<myString.length; i++)
    {
        // check next letter in the originalString
        var currentLetter:String = originalString.charAt(i);

        // increase the lettersFound if the currentLetter is the next letter in our word. This also means that next time we will check for the next letter in the word
        if (currentLetter == word.charAt(lettersFound))
        {
            lettersFound++;
        }
    }

    // return true if the lettersFound equals the length of our word (meaning we've found all letters)
    return lettersFound == word.length;
}

var stringContainsWord:Boolean = containsWord("wqeeqwejqwqwqaretrtviyiiyia", "java");
trace(stringContainsWord);