Groups | Blog | Home
all groups > flash actionscript > march 2006 >

flash actionscript : retrieving substring from a string


DMennenoh *TMM*
3/6/2006 6:04:50 PM
Look up the string class in the help...

--
Dave -
www.blurredistinction.com
www.macromedia.com/support/forums/team_macromedia/

Antonio_82_
3/6/2006 10:38:06 PM
hello everyone, does anyone know how to extract in actionscript, given a string, say:
var str = "Pittsburgh";
the last 5 letters ("burgh")?
thanks
Antonio_82_
3/7/2006 12:00:00 AM
blemmo
3/7/2006 12:09:02 AM
var str = "Pittsburgh";
var substr = str.substr(str.length-5);

substr returns a substring of the given string from parameter 1 until an
optional parameter 2 or the end. The last 5 letters start at str.length-5, so
substr(str.length-5) returns the last 5.

cheers,
blemmo

NSurveyor
3/7/2006 12:11:23 AM
Look up the String methods in the AS reference of the Help panel.

var str = "Pittsburgh";
var x = str.slice(-5)
trace(str);
trace(x);

is one way of accomplishing this.

You could have also used substr, substring, or slice like above:

trace(str.substr(-5));
trace(str.substring(str.length-5));
trace(str.slice(-5));

Using negatives is a nice way to start 5 from the end... but in other cases,
one of the three methods above would be better in certain situations... I
suggest you read their entries in the Help panel.
blemmo
3/7/2006 12:21:25 AM
Originally posted by: NSurveyor
Using negatives is a nice way to start 5 from the end...
Rightyright... saves typing xxx.length everytime. I didn't know substr(-5) is
enough before... thanks from my lazy fingers for mentioning this :)

cheers,
blemmo


jonnybennett
3/7/2006 1:11:27 AM
Okay in a similar problem I have this...

username='bernard';
userLength=username.length-1;
str="bernard24.jpg";
substr= str.substr(str.length-userLength);
substr2=substr.split(".");
var newNumber:Number=substr2;
newNumber2=newNumber+'1';
newJpg=username+newNumber2+'.jpg';
trace(newJpg);

I want it to output bernard25.jpg....
but it is still considering newNumber2 to be a string, so when you add 1, you
get 241... therefore a final output of
bernard241.jpg

Anyone know how to make it so that it recognises newNumber2 as a number and
not string?.

blemmo
3/7/2006 1:23:43 AM
johnny, you could use parseInt(substr2) to get a number type, or use
newNumber2=++newNumber, the ++ or -- works also with numerical strings.
Currently, you are adding the string "1", so it would result in "bernard241"
even if you had a numerical var newNumber. This should be numeric, without
quotes.

hth,
blemmo

jonnybennett
3/7/2006 1:31:12 AM
Cheers blemmo, I had taken out the quotes before I got your response, but it
still didn't work, although that obviously wasn't helping!!!... however the
following code does work.....

username='bernard';
userLength=username.length-1;
str="bernard24.jpg";
substr=str.substr(str.length-userLength);
substr2=substr.split(".");
newNumber=substr2;
newNumber=Number(newNumber);
//trace(test + 10)
newNumber2=(newNumber+1);
newJpg=username+newNumber2+'.jpg';
trace(newJpg);

I've not had a chance to look at your way of using ++ on numeric strings...
i'd imagine it would be better...
thanks again for your help.
blemmo
3/7/2006 1:48:00 AM
Just use
newNumber2=++newNumber;

instead of
newNumber=Number(newNumber);
newNumber2=(newNumber+1);

Looks a bit strange, but as the ++ operator works on numeric strings, it would
work here. ("24"++) would be "25", but after newNumber2 was assigned, so that's
why ++newNumber to add before assigning.
Dunno if it's better than casting to a number... it feels a bit 'dirty', but
works and doesn't cast anything, so maybe it's more efficient.

greets,
blemmo

AddThis Social Bookmark Button