Be a Supporter!
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
AS: Strings 2005-08-30 13:23:30 Reply

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.

23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to AS: Strings 2005-08-30 13:54:18 Reply

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
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Strings 2005-08-30 13:58:25 Reply

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.

FreedomSlave
FreedomSlave
  • Member since: Sep. 27, 2001
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to AS: Strings 2005-08-30 14:00:01 Reply

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.

Begoner
Begoner
  • Member since: Oct. 10, 2004
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to AS: Strings 2005-08-30 14:00:52 Reply

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?

XTra-KrazzY
XTra-KrazzY
  • Member since: Jul. 29, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to AS: Strings 2005-08-30 14:00:56 Reply

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.

23450
23450
  • Member since: May. 28, 2003
  • Offline.
Forum Stats
Supporter
Level 27
Blank Slate
Response to AS: Strings 2005-08-30 14:04:32 Reply

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
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Strings 2005-08-30 14:05:27 Reply

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.

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Strings 2005-08-30 14:17:37 Reply

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
Inglor
Inglor
  • Member since: Jan. 26, 2003
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to AS: Strings 2005-08-30 14:19:49 Reply

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

T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Strings 2005-08-30 14:25:09 Reply

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?

Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to AS: Strings 2005-08-30 14:41:18 Reply

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

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Strings 2005-08-30 14:57:44 Reply

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
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Strings 2005-08-30 15:04:16 Reply

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.


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Strings 2005-08-30 15:06:34 Reply

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


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Strings 2005-08-30 15:08:27 Reply

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
Rustygames
Rustygames
  • Member since: May. 7, 2005
  • Offline.
Forum Stats
Member
Level 19
Programmer
Response to AS: Strings 2005-08-30 15:10:01 Reply

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

dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Strings 2005-08-30 15:10:31 Reply

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?


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Strings 2005-08-30 15:11:41 Reply

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
dELtaluca
dELtaluca
  • Member since: Apr. 16, 2004
  • Offline.
Forum Stats
Member
Level 20
Blank Slate
Response to AS: Strings 2005-08-30 15:13:35 Reply

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 :(


using ShamelessPlug; NapePhysicsEngine.advertise();

BBS Signature
Afro-Ninja
Afro-Ninja
  • Member since: Mar. 2, 2002
  • Offline.
Forum Stats
Moderator
Level 44
Game Developer
Response to AS: Strings 2005-08-30 15:14:15 Reply

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
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to AS: Strings 2005-08-30 15:15:10 Reply

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
T-H
T-H
  • Member since: Jan. 7, 2004
  • Offline.
Forum Stats
Member
Level 40
Blank Slate
Response to AS: Strings 2005-08-30 15:25:09 Reply

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

fwe
fwe
  • Member since: Jul. 24, 2003
  • Offline.
Forum Stats
Member
Level 08
Blank Slate
Response to AS: Strings 2005-08-30 16:30:33 Reply

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


wtfbbqhax