hello everyone, does anyone know how to extract in actionscript, given a string, say: var str = "Pittsburgh"; the last 5 letters ("burgh")? thanks
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
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.
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
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?.
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
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.
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
Don't see what you're looking for? Try a search.
|