Solved

How do you test for substrings in Actionscript?

Posted on 2008-06-25
3
210 Views
Last Modified: 2013-11-11
Using pseudocode now, I want the following:

var1 = "foobar"

If (var1 contains "foo") {
    result = True;
}else{
    result =  False;
}


I dont know where "foo" will appear in my test variable var1, so not looking for

substring(start:Number, end:Number) : String

unless there's another way to implement this.
0
Comment
Question by:kharndog
  • 2
3 Comments
 
LVL 43

Accepted Solution

by:
Rob earned 500 total points
ID: 21863916
what about indexOf?


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

Open in new window

0
 
LVL 43

Expert Comment

by:Rob
ID: 21863929
so to use your example
var var1:String = "foobar"
var result:Boolean = false;
 
result = (var1.indexOf("foo") >= 0);

Open in new window

0
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 21864059
as tagit mentioned:
actionscript uses "indexOf()" method of string in place of "contains"


below is the exact conversion of your code from  pseudocode to actionscript:

var1 = "foobar";
If (var1.indexOf("foo")!=-1) {
    result = True;
}else{
    result =  False;
}

if returned value is equal to -1, that's mean substring does not exist in main string.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times in your Flash CS4 application when you want more than a simple pointer or a hand, and it's hard to find an ideal walk-through to tell you what to do.  I spent a few days recently learning my way around making custom cursors in Flash,…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question