PDA

View Full Version : left() and right() functions in Flash?



Mannequin
March 26th, 2004, 09:31 AM
I've searched Google and several Flash forums but I haven't found this topic. I'm probably not searching for the right terms, although I've tried several. Here's my problem:

In most web-based programming languages you can use the left() and right() functions to move through a string and pull out individual characters. In ColdFusion, I could do this:

left("hello", 1) and it would produce "h"

I could also do:

right("hello", 1) and it would produce "o"

Is there a conversion for this method in Flash? I want to pick the first letter of a string and capitalize it.

In ColdFusion I would do it like this:

uCase(left("hello", 1)) -- and it would give me: "H"

Any help would be appreciated.
Thanks.

khamstra
March 26th, 2004, 10:54 AM
Here is a method for capitalizing the first letter of a string.
myWord = "string";
myFirstLetter = myWord.substr(0,1).toUpperCase();

Hope that helps, khamstra

khamstra
March 26th, 2004, 11:05 AM
You could also add the left and right functions to all MovieClips and use it anywhere in the application.

MovieClip.prototype.left = function(string, count) {
if(count == undefined) var count = 1;
return string.substr(0, count);
};
MovieClip.prototype.right = function(string, count) {
if(count == undefined) var count = 1;
var s = string.length-count;
var f = string.length;
return string.substring(s, f);
};
Then you could use it as follows

myWord = "string";
myFirstLetter = left(myWord).toUpperCase();
trace(myFirstLetter);
myLastLetter = right(myWord);
trace(myLastLetter);
myLast3Letters = right(myWord,3);
trace(myLast3Letters);
Hope that helps, khamstra

Voetsjoeba
March 26th, 2004, 01:57 PM
Why use MovieClip.prototype :h:



String.prototype.left = function(count) {
if (count == undefined) {
var count = 1;
}
return this.substr(0, count);
};
String.prototype.right = function(count) {
if (count == undefined) {
var count = 1;
}
var s = this.length-count;
var f = this.length;
return this.substring(s, f);
};
myWord = "string";
myFirstLetter = myWord.left();
trace(myFirstLetter);
myLastLetter = myWord.right();
trace(myLastLetter);
myLast3Letters = myWord.right(3);
trace(myLast3Letters);

senocular
March 26th, 2004, 02:20 PM
also note:

http://proto.layer51.com/l.aspx?p=2

with such examples like:

http://proto.layer51.com/d.aspx?f=320

khamstra
March 26th, 2004, 02:29 PM
I initially did it with String instead of MovieClip. Then I looked at what he was asking for so I set it up just like ColdFusion works.

Also with String you have to set the variable first with my method you could easily just do this.

right("string", 3);

Thats why I used MovieClip, khamstra

Mannequin
March 26th, 2004, 05:52 PM
Thanks guys. These are all very useful examples.