00:00
00:00
Newgrounds Background Image Theme

Breakfast-Crow just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Strings 2005-08-30 13:23:30


AS: Main, because AS: is better then void

This will cover the String object data type, it's explained in
AS: Variables by Rantzien but the String object has a lot of methods primitive variables don't.

What are strings?

Strings are used to store a lot of characters, specifically, words , text and other word-related things. for example "Hello" is a string, "Welcome Home" is a string, "I love pie and I love" + newline + "LOLZ" is a string

I will devide this tutorial to 3 parts,
1)What ARE strings?
2)String uses
3String functions

What strings are

Strings are really constant character arrays, what does it mean? it means that it's an ordered bunch of letters, a character is a single letter and an array is a bunch of variables one after another... the fact that it's constant means that whenever you modify an array you actually distroy it and recreate it modified,

For what you generally need to know Strings are words.

Another importent thing you need to know about Strings in flash is that a string in flash is an Object, it has properties like it's actual content and other importent properties like "lentgh", and methods like "slice" "join" and others

Usage

To declare a string you simple use the var command like you do with a var

var string_name; //partially

var string_name:String; //fully

you can also add a value to it like so:

var string_name:String= "Hello";

Strings are used for many things, names, message boxes, and generally any form of text you store. Since strings are primitive objects in flash tehy have very easy to use functions.

String Functions

The 2 things you'll probebly use the most with strings, are the "+" operator, and the charAt method.

The + operator

With strings, the addition (+) operator is used to append one string for another, for example "hello" + " world" would be equal to writing "hello world", you can do it with string variables too:

var s:String="hello";
var sa:String=" world";

trace(s+sa); //traces "hello world"
trace(sa+s); //traces " worldhello"
trace("hello" + s);//traces "hellohello"

youy can also do
s=s+sa;
making s equal now to "hello world";
or in short:
s+=sa;
producing the same effect.

charAt
sometimes you'll want to access a specific character in a string, this is done with the String.charAt(position); command, you can just do name.charAt(0) for the first char or for example s.charAt(2); for the 3rd char, in this case 'l'.

The others summerized from liveDocs

An importent property you'll use is String.length it contains the length of a string, for example s.length would return 5.

String.charCodeAt() Returns the value of the character at the specified index as a 16-bit integer between 0 and 65535.

String.concat() Combines the text of two strings and returns a new string. for example s.concat(sa) would return hello world, just like the + operator.

String.fromCharCode(index:Number) Returns a string comprising the characters specified in the parameters.

String.indexOf() Returns the position of the first occurrence of a specified substring. for example s.indexOf("l"); would return 2 since it's the first place it appears, take note that index is 1 character before the location.

String.lastIndexOf() Returns the position of the last occurrence of a specified substring. s(lastIndexOf("l"); would return 3, because it's the last place "l" appears, take note that index is 1 character before the location.

String.slice() Extracts a section of a string and returns a new string. for example s.slice(3) would return lo, s.slice(1,3) would return elo

String.split() Splits a String object into an array of strings by separating the string into substrings. this one is by a specific character, look into it's livedocs, it's importent and worthy to check out.

String.substr() Returns a specified number of characters in a string, beginning at a specified location.

String.substring() Returns the characters between two indexes in a string.

String.toLowerCase() Converts the string to lowercase and returns the result; does not change the contents of the original object.

String.toUpperCase() Converts the string to uppercase and returns the result; does not change the contents of the original object.

Response to AS: Strings 2005-08-30 13:54:18


I have a question. Why do we use var s:String="hello";? Cant we just use s="hello" and work the same way? There must be a reason to make it complicated and not keep it simple.


BBS Signature

Response to AS: Strings 2005-08-30 13:58:25


At 8/30/05 01:54 PM, 23450 wrote: I have a question. Why do we use var s:String="hello";? Cant we just use s="hello" and work the same way? There must be a reason to make it complicated and not keep it simple.

Because if you dont use strict data typing you are a very very naughty boy.

Response to AS: Strings 2005-08-30 14:00:01


At 8/30/05 01:54 PM, 23450 wrote: I have a question. Why do we use var s:String="hello";? Cant we just use s="hello" and work the same way? There must be a reason to make it complicated and not keep it simple.

It's just strict data typing, if you were coding in AS 2 it's beneficial (maybe vital) to use strong typing on all of your variables. if you declare it as s:String = "whatever" then it makes sure that that var will only store a string, meaning if you make a mistake and accidently assign it a number or some such, Flash will throw back and error.

It's good coding practice in other words.

Response to AS: Strings 2005-08-30 14:00:52


Nice tutorial, Inglor!

Is there any way to evaluate the a string as an expression? Let's say you have a string:

"x = Math.sin( 45 )"

And you want it to be interpreted as an expression, not as a string. In many other languages, there's an eval() function that does exactly that. However, Flash's eval() function does nothing of the sort. Is there a function do to this?

Response to AS: Strings 2005-08-30 14:00:56


At 8/30/05 01:58 PM, T-H wrote:
At 8/30/05 01:54 PM, 23450 wrote: I have a question. Why do we use var s:String="hello";? Cant we just use s="hello" and work the same way? There must be a reason to make it complicated and not keep it simple.
Because if you dont use strict data typing you are a very very naughty boy.

PWNED.

Anyways, nice tutorial! it covers every string function I can think of right now.

Response to AS: Strings 2005-08-30 14:04:32


At 8/30/05 02:00 PM, Freedom_Slave wrote: It's good coding practice in other words.

Well until i start having problemns with it, and until i get 8Ball, i will keep it simple. Right now i only code in AS1. So thanks for the input.


BBS Signature

Response to AS: Strings 2005-08-30 14:05:27


At 8/30/05 02:00 PM, Begoner wrote: And you want it to be interpreted as an expression, not as a string. In many other languages, there's an eval()

nope, I checked, and I ended up writing my own parser... flash doesn't offer that.

Response to AS: Strings 2005-08-30 14:17:37


At 8/30/05 01:58 PM, T-H wrote:
At 8/30/05 01:54 PM, 23450 wrote: I have a question. Why do we use var s:String="hello";? Cant we just use s="hello" and work the same way? There must be a reason to make it complicated and not keep it simple.
Because if you dont use strict data typing you are a very very naughty boy.

I never use strict data typing =S


- - Flash - Music - Images - -

BBS Signature

Response to AS: Strings 2005-08-30 14:19:49


that's why you get lag in games like caliche :P

Response to AS: Strings 2005-08-30 14:25:09


At 8/30/05 02:17 PM, Denvish wrote: I never use strict data typing =S

I always use it, got into habit from the start of leaning AS. As you started a long time before me I guess you stuck with different habits.

Basic var declaration instead of strict can't cause lag can it Inglor?

Response to AS: Strings 2005-08-30 14:41:18


At 8/30/05 02:17 PM, Denvish wrote:
At 8/30/05 01:58 PM, T-H wrote:
At 8/30/05 01:54 PM, 23450 wrote: I have a question. Why do we use var s:String="hello";? Cant we just use s="hello" and work the same way? There must be a reason to make it complicated and not keep it simple.
Because if you dont use strict data typing you are a very very naughty boy.
I never use strict data typing =S

GO SIT IN THE CORNER AND THINK ABOUT WHAT YOUVE DONE DENVISH YOU NAUGHTY BOY!


- Matt, Rustyarcade.com

Response to AS: Strings 2005-08-30 14:57:44


At 8/30/05 02:41 PM, Ninja-Chicken wrote: GO SIT IN THE CORNER AND THINK ABOUT WHAT YOUVE DONE DENVISH YOU NAUGHTY BOY!

Make me.

AS: Strings


- - Flash - Music - Images - -

BBS Signature

Response to AS: Strings 2005-08-30 15:04:16


At 8/30/05 02:25 PM, T-H wrote:
At 8/30/05 02:17 PM, Denvish wrote: I never use strict data typing =S
I always use it, got into habit from the start of leaning AS. As you started a long time before me I guess you stuck with different habits.

Basic var declaration instead of strict can't cause lag can it Inglor?

s = "Hello" is a very very naughy thing to do
var s = "Hello" is fine for AS1 and AS2
var s:String = "Hello" is AS2 strict-data typing

var s = "Hello" is just a declaration
var s:String = "Hello" gives the var a data-type, this increases performance and is useful for reasons mentioned: I believe that it increases performance since when flash handles these vars it knows what data type the var is, whereas as otherwise it would first determine what type of data it is (i dont know if this is true or not) which is obviously lessens performance.

Response to AS: Strings 2005-08-30 15:06:34


woot. just realised that was my 666th post!!

Response to AS: Strings 2005-08-30 15:08:27


At 8/30/05 03:06 PM, dELta_Luca wrote: woot. just realised that was my 666th post!!

Congratulations, evil dude!

AS: Strings


- - Flash - Music - Images - -

BBS Signature

Response to AS: Strings 2005-08-30 15:10:01


At 8/30/05 02:57 PM, Denvish wrote:
At 8/30/05 02:41 PM, Ninja-Chicken wrote: GO SIT IN THE CORNER AND THINK ABOUT WHAT YOUVE DONE DENVISH YOU NAUGHTY BOY!
Make me.

Is that you?
I wouldnt dare sir
* Wimpers


- Matt, Rustyarcade.com

Response to AS: Strings 2005-08-30 15:10:31


At 8/30/05 03:08 PM, Denvish wrote:
At 8/30/05 03:06 PM, dELta_Luca wrote: woot. just realised that was my 666th post!!
Congratulations, evil dude!

whats with the [ADMINISTRATE] part?

Response to AS: Strings 2005-08-30 15:11:41


At 8/30/05 03:10 PM, dELta_Luca wrote: whats with the [ADMINISTRATE] part?

That's just there in case I fancy banning you =)
You have no [RECORDS] though, so you're all right...


- - Flash - Music - Images - -

BBS Signature

Response to AS: Strings 2005-08-30 15:13:35


At 8/30/05 03:11 PM, Denvish wrote:
At 8/30/05 03:10 PM, dELta_Luca wrote: whats with the [ADMINISTRATE] part?
That's just there in case I fancy banning you =)
You have no [RECORDS] though, so you're all right...

[RECORDS] ? now im just confused :(

Response to AS: Strings 2005-08-30 15:14:15


At 8/30/05 03:13 PM, dELta_Luca wrote:
At 8/30/05 03:11 PM, Denvish wrote:
At 8/30/05 03:10 PM, dELta_Luca wrote: whats with the [ADMINISTRATE] part?
That's just there in case I fancy banning you =)
You have no [RECORDS] though, so you're all right...
[RECORDS] ? now im just confused :(

[RECORDS] show's a user's last recent ban


BBS Signature

Response to AS: Strings 2005-08-30 15:15:10


At 8/30/05 03:13 PM, dELta_Luca wrote:
That's just there in case I fancy banning you =)
You have no [RECORDS] though, so you're all right...
[RECORDS] ? now im just confused :(

They're just links that mods can see. The records link shows the past ban details on rollover. And I'd better not say any more than that.


- - Flash - Music - Images - -

BBS Signature

Response to AS: Strings 2005-08-30 15:25:09


At 8/30/05 03:14 PM, Afro_Ninja wrote: [RECORDS] show's a user's last recent ban

All is revealed.

BIG BROTHER IS WATCHING YOU

Fucking conspiracy

Response to AS: Strings 2005-08-30 16:30:33


Haha, go to my records and tell me how many times i've been banned


wtfbbqhax