Monday 17 August 2009

Strings and Arrays

Continuing my last message about "String operations" some more usefull functions to use with strings. These allow you to receive a all string and separate them into data chunks that you can put in a array. It's usefull if get data like a cvs file, where all data is separated with a comma, ou semicollom(;)

var rainbow:String="red;orange;yellow;green;cyan;blue;violet";

1. Split string into an array using the (;)
trace(rainbow.split(";"));
or
var colourArray:Array=rainbow.split(";");

This will split the string in all the colors that are separated by the ";" symbol, and put them in a array

2. Split string a few times only
trace(rainbow.split(";",3));
or
var colourArray:Array=rainbow.split(";",3);

This will do the same as above, but limited to three splits

========================================

There's another operation that is even more powerfull... the combination of Split with Join.
This allow you to search for a word in text and replace it with another

1. To remove a repeating word in text
var rainbow:String="red then comes orange then comes yellow then comes green then comes cyan then comes blue then comes violet";

rainbow=rainbow.split(" then comes ").join("");

trace(rainbow);

2. Replacing words
var rainbow:String="red then comes orange then comes yellow then comes green then comes cyan then comes blue then comes violet";

rainbow=rainbow.split(" then comes ").join(";");

trace(rainbow);

String operations

Dealing with String sometimes gets you wonder "How can I split this?", "Can I extract that?".

Well, AS3 has some pretty function that manage to do this, but sometimes, the documentation gets you wonder how practical it is.

So here are some examples for common operations with Strings:

My string:

var myString:String="Always speak the truth, think before you speak, and write it down afterwards.";

==============================================

1. Show the first character
var fstChar:String=myString.charAt(0);

2. Output the first 6 characters
var sixChar:String=myString.substring(0,6);

3. Output from the sixth character
var fromSixth:String=myString.substring(6);

4. Slice the string from the 8th to the 12th character (basically will to the same has the substring)
var eigthToTwelve:String=myString.slice(8,12);

5. Search for a word and output it's position
var search:String=myString.search("truth");

6. Convert all letters to lower case
var lowerCase:String=myString.toLowerCase();

7. Convert all letters to upper case
var upperCase:String=myString.toUpperCase();

8. Length of a string
var howBig:Number=myString.length;

9. Concatenation... or adding something to the string (String are not like numbers, you can't add an "a" to a "b", so the operation "a"+"b" will result in "ab")

10. Replacing text with other text (this might get trickier)
var rplcText:String=myString.replace(myString.substring(0,6),"sometimes");
(this will replace the word "Always", wich is between character 0 and 6, and replace it with the word "sometimes")

Hope it helps someone.

Welcome

Hi everybody,

I thought of creating this blog, mostly because there are small pieces of code, that can be recycled to different situations.
We all can find some pieces of code and keep a bookmark for the web page that contains it, but sometimes it's not praticall to have scattered pages with the code we need, and some pages, will eventually, be offline or disappear.

So with this in mind I'me creating this blog to help me, and all of you that will find some of the snippets usefull.

Althought it's called "AS3 Snippets", wich is main programming language that I use, it's not exclusive to it. If you think some sort of snippet will be usefull to everyone, please email it to me, to include.

Thank you.