开发者

Finding the position of occurrence of a word in a string

开发者 https://www.devze.com 2023-03-12 04:35 出处:网络
I have a pipe delimited string \"Y|Y|Y|N|N开发者_如何学编程\". How can I find the position of first occurrence of N using code after processing the string.It looks like your question should read:

I have a pipe delimited string "Y|Y|Y|N|N开发者_如何学编程". How can I find the position of first occurrence of N using code after processing the string.


It looks like your question should read:

"I have a pipe delineated string. I want the index of something in the string once the pipes have been removed"

// first occurrence of string "N" if you simply remove the pipes first.
var str:String = "Y|Y|Y|N|N";
var index:int = str.replace("|","").indexOf("N");

Because removing the pipes can actually concatenate strings and lead to false positives, you may want to consider the following:

var str:String = "Y|Y|Y|N|N";
var pieces:Array = str.split("|");
for( var i:int = 0; i < pieces.length; i++ )
{
    if( pieces[ i ] == "N" ) break;
}
// i will now be the first index.

if you're just looking for removal of the pipe, str.replace works in AS3, in AS2 you need to use str.split("|").join("");


AS3 supports a lot of such simple tasks,

For your problem simply use:

str.indexOf("N");

where str is your string.

Read more about string functions, here.


EDIT:

That's not a problem either, that's why I linked you to as3 docs.You will hardly need regular expressions.

str.replace("|","").indexOf("N");


http://livedocs.adobe.com/flex/3/html/help.html?content=12_Using_Regular_Expressions_01.html

Your expression will be something like "/Y\|Y\|Y\|(.)\|.\|/" (I haven't tested this...)

A good place to test it: http://ryanswanson.com/regexp/#start


I hope I understood you correctly... Have you read http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary692.html?

indexOf returns the position of the substring you search for in the string.

0

精彩评论

暂无评论...
验证码 换一张
取 消