Forum Topic: AS: Strings

(2,105 views • 23 replies)

This topic is 1 page long.

<< < > >>
None

Inglor

Reply To Post Reply & Quote

Posted at: 8/30/05 01:23 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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.


None

23450

Reply To Post Reply & Quote

Posted at: 8/30/05 01:54 PM

23450 LIGHT LEVEL 27

Sign-Up: 05/28/03

Posts: 7,301

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.

Halo 3 GamerTag: XCornDawgX


None

T-H

Reply To Post Reply & Quote

Posted at: 8/30/05 01:58 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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.


None

FreedomSlave

Reply To Post Reply & Quote

Posted at: 8/30/05 02:00 PM

FreedomSlave NEUTRAL LEVEL 18

Sign-Up: 09/27/01

Posts: 795

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.


None

Begoner

Reply To Post Reply & Quote

Posted at: 8/30/05 02:00 PM

Begoner NEUTRAL LEVEL 10

Sign-Up: 10/10/04

Posts: 3,038

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?


None

XTra-KrazzY

Reply To Post Reply & Quote

Posted at: 8/30/05 02:00 PM

XTra-KrazzY EVIL LEVEL 09

Sign-Up: 07/29/05

Posts: 53

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.


None

23450

Reply To Post Reply & Quote

Posted at: 8/30/05 02:04 PM

23450 LIGHT LEVEL 27

Sign-Up: 05/28/03

Posts: 7,301

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.

Halo 3 GamerTag: XCornDawgX


None

Inglor

Reply To Post Reply & Quote

Posted at: 8/30/05 02:05 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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.


None

Denvish

Reply To Post Reply & Quote

Posted at: 8/30/05 02:17 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,229

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

None

Inglor

Reply To Post Reply & Quote

Posted at: 8/30/05 02:19 PM

Inglor NEUTRAL LEVEL 17

Sign-Up: 01/26/03

Posts: 10,948

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


None

T-H

Reply To Post Reply & Quote

Posted at: 8/30/05 02:25 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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?


None

Rustygames

Reply To Post Reply & Quote

Posted at: 8/30/05 02:41 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,661

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


None

Denvish

Reply To Post Reply & Quote

Posted at: 8/30/05 02:57 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,229

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

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 8/30/05 03:04 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,553

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.

My social worker says im special!

BBS Signature

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 8/30/05 03:06 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,553

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

My social worker says im special!

BBS Signature

None

Denvish

Reply To Post Reply & Quote

Posted at: 8/30/05 03:08 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,229

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

None

Rustygames

Reply To Post Reply & Quote

Posted at: 8/30/05 03:10 PM

Rustygames LIGHT LEVEL 18

Sign-Up: 05/07/05

Posts: 6,661

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


None

dELtaluca

Reply To Post Reply & Quote

Posted at: 8/30/05 03:10 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,553

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?

My social worker says im special!

BBS Signature

None

Denvish

Reply To Post Reply & Quote

Posted at: 8/30/05 03:11 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,229

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

None

dELtaluca

Reply To Post Reply & Quote

Posted at: 8/30/05 03:13 PM

dELtaluca LIGHT LEVEL 20

Sign-Up: 04/16/04

Posts: 5,553

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

My social worker says im special!

BBS Signature

None

Afro-Ninja

Reply To Post Reply & Quote

Posted at: 8/30/05 03:14 PM

Afro-Ninja EVIL LEVEL 38

Sign-Up: 03/02/02

Posts: 13,463

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

None

Denvish

Reply To Post Reply & Quote

Posted at: 8/30/05 03:15 PM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,229

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

None

T-H

Reply To Post Reply & Quote

Posted at: 8/30/05 03:25 PM

T-H LIGHT LEVEL 39

Sign-Up: 01/07/04

Posts: 4,893

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


None

fwe

Reply To Post Reply & Quote

Posted at: 8/30/05 04:30 PM

fwe DARK LEVEL 08

Sign-Up: 07/24/03

Posts: 3,361

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

wtfbbqhax


All times are Eastern Standard Time (GMT -5) | Current Time: 03:25 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!