01-14-2011 07:32 PM
Hi,
Is there any way in ActionScript to compare strings?
Normally in Java there is a .equals() method.
What about in AS?
i want to check if let's say, if x is equal to "PlayBook".
G
01-14-2011 07:57 PM
You could compare string lengths and then check that the index of the first string in the second one is zero, using the indexOf() String method. Also, you can pre-trim the strings with the mx.utils.StringUtil.trim() static class method.
01-14-2011 08:07 PM
BTW, I'm also new to AS3 and from your question I assumed you'd looked for an "==" operator & not found one. However now that I look closer, since I'll need to do string comparisons as well, according to p. 133 of this:
http://livedocs.adobe.com/flex/3/progAS_flex3.pdf
"You can use the following operators to compare strings: <, <=, !=, ==, =>, and >."
PDF is 500+ pages of AS3 documentation straight from the horse's mouth.
01-14-2011 08:11 PM
String.search(); String.match(); String.indexOf(); String.lastIndexOf();
Any of the above could work, depending on your use case.
01-14-2011 10:01 PM
UberschallSamsara wrote:
PDF is 500+ pages of AS3 documentation straight from the horse's mouth.
Thanks for the link - never seen it before - really useful reference!!!!
01-15-2011 05:06 AM - edited 01-15-2011 06:14 AM
trace("Playbook" == "playbook"); //false
trace("Playbook" != "playbook"); //true
trace("This is stupid easy" != "This is stupid easy"); //false
trace("This is stupid easy" == "This is stupid easy"); //true
if you're trying to determine the OS at runtime, something like the following should work for you:
//Resoslve Operating System
private function resolveOperatingSystem():String
{
if (Capabilities.os.toLowerCase().indexOf("win") > -1) return "Windows";
else if (Capabilities.os.toLowerCase().indexOf("mac") > -1) return "Mac";
else if (Capabilities.os.toLowerCase().indexOf("qnx") > -1) return "PlayBook";
else if (Capabilities.manufacturer.toLowerCase().indexOf(" and") > -1) return "Android";
else if (Capabilities.os.toLowerCase().indexOf("iph") > -1) return "iPhone";
}
01-15-2011 12:30 PM
Unlike Java ActionScript actually lets you compare strings the exact same way you compare everything else. I for one, do not miss the clunky .equals() method one bit...