Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

How do you test for substrings in Actionscript?

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
kharndog
Asked:
kharndog
  • 2
1 Solution
 
RobOwner (Aidellio)Commented:
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
 
RobOwner (Aidellio)Commented:
so to use your example
var var1:String = "foobar"
var result:Boolean = false;
 
result = (var1.indexOf("foo") >= 0);

Open in new window

0
 
Aneesh ChopraCommented:
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now