AS3 - 查找位于两个其他子字符串之间的子字符串

时间:2012-01-14 03:07:41

标签: actionscript-3 substring

我正在创建一个Flash应用程序,它从服务器加载文件,并将其存储在字符串中。我想找到该字符串中的子字符串,它位于两个已知子字符串之间。

请注意。我不想用reg-ex这样做,因为我希望它可以轻松重复使用。

理想情况下,我想要一个可以重复使用的函数,通过传递开始和结束子字符串。

e.g。沿着这些路线......

function getSubString(start:String, end:String):String

我在这个问题上挣扎。如果有人能帮忙,我会非常感激。谢谢。

毫无疑问,这是其中一个得到答案的问题“为什么你不只是谷歌...”。我做了,并没有找到太多!对不起!

编辑:字符串中可能有多个'start'和'end'子串的实例,我希望它在每个子实例的第一个实例之间。

1 个答案:

答案 0 :(得分:2)

function getSubString(start:String, end:String, fullString:String):String {
    var startIndex:Number = fullString.indexOf(start) + start.length;
    var endIndex:Number = fullString.indexOf(end) - 1;// You can change this to 
                                                      // lastIndexOf in order to
                                                      // get the string between the 
                                                      //first instance of start and
                                                      // the last index of end
    return fullString.substring(startIndex, endIndex);
}

我没有测试过,所以你可能会发现一个错误。

相关问题