Useful String Functions with ActionScript 3.0

Useful String Functions with ActionScript 3.0

Useful String Functions with ActionScript 3.0

Some useful ActionScript String functions that can be used in ActionScript 3.0 projects …
When working a lot with Strings in ActionScript you would really want to check out following String operations for your Flash (AS3) project.

Overview of the different functions: stripHttp, capitalise a full strong, capitalise the First Letter of a String, getNumBytesUTF8, email Validation, space trimmer and getting the last char of a string.

Strip Spaces from a String

private function stripSpaces($myString:String):String
{
    return $myString.split(" ").join("");
}

Get the Last Char in a String

private function getLastCharInString($s:String):String
{
    return $s.substr($s.length-1,$s.length);
}

Email Validation

private function isValidEmail($email:String):Boolean {
var emailExpression:RegExp = /([a-z0-9._-]+?)@([a-z0-9.-]+)\.([a-z]{2,4})/;
    return emailExpression.test($email);
}

Number of bytes in a UTF8 String

private function getNumBytesUTF8 ($s:String):Number {
var byteArray:ByteArray = new ByteArray();
byteArray.writeUTFBytes($s);
    return byteArray.length;
}

Capitalise the First Letter of a String

function catipaliseFirstLetter($s:String):String {
    return $s.substring(0, 1).toUpperCase() + $s.substr(1, $s.length-1);
}

Capitalise a String

function catipaliseString($s:String):String {
    return $s.toUpperCase();
}

Strip http:// or https://

function stripHttp($string:String, $stripWWW:Boolean = true):String
{
    var regexp:RegExp = new RegExp(!$stripWWW ? "https*:\/\/" : "https*:\/\/(www\.)*", "ig");
    return $string.replace(regexp, "");
}

These are some common String manipulation functions that we frequently use in different AS3 projects.
If you have any additions to these String functions? Use out comment box and we will add them!


About the Author

Daan is a Creative-Geek who loves learning and sharing new techniques! Follow him on Twitter to keep up to date with the Creative-Geeks blog and other subjects. Contact him on e-mail : info[at]creative-geeks.com.