A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Rmove characters from a variable

  1. #1
    Senior Member
    Join Date
    Oct 2002
    Posts
    273

    Rmove characters from a variable

    I have a variable which includes letters and numbers. I need to use the numbers in the varible, and strip out the letters. For example, the value of my variable is

    "item1"

    I need to get rid of the "item" and create a new varible which contains only the "1"

    Any ideas?

  2. #2
    Junior Member
    Join Date
    Oct 2002
    Posts
    14

    Solution to UR problem.

    Hi JayCharles,

    I think U can do this....

    using the for loop, check for all the characters in the variable. and for each individual character check for its ascii value. if it falls between 0 and 9, place this characters in a new variable and then convert back to numeric.

    Hope this solves ur problem.

    Happy Coding,
    b_san.

  3. #3
    Senior Member
    Join Date
    Oct 2002
    Posts
    273
    Sorry... I'm a bit new to flash and I don't have a grip of the for loop functions.

    Any chance you could help me out with a few lines of code?

    What I have is a variable named "last". It's value is "item1". Can you show me how to strip out the "item" and leave just the "1" ?

    thanks.

  4. #4
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,358
    http://www.layer51.com/proto/ might have some functions for that

    They are actually prototypes. Since you are new to flash Ill quickly try to explain what prototypes are

    Prototypes are a kind of function, or a set of code which can be used over and over again to perform the same action. The special thing about prototypes are that they are associated with a certain kind of object. A string for example is an "object", a string object. A string prototype adds extra functionality to a string you might be using in flash. Flash already has its built in string prototypes like .toLowerCase(), .charAt(), .slice() etc. These are all functions that are used specifically on strings. The way you make your own versions of these is by writing your own prototpe function in the form (for a string):

    String.prototype.prototypeName = function(){
    ...
    }

    Then, after flash reads that code you are able to use that new function on all your strings.

    myString = "hello";
    myString.prototypeName()

    and whatever the prototypeName function does will be performed on that string, or atleast be returned as a result.

    So there ya go, low down and dirty prototype introduction. Hopefully http://www.layer51.com/proto/ will have something for you... if not I can whip something up as well.

  5. #5
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,358
    ok I went through real quick and didnt see any that you wanted exactly (though I could have missed one). I did see one that parsed or got the first occurance of a number from a string, but not all the numbers. You could probably just as well use that, especially with the item1 example, but just incase, heres another one.

    Code:
    String.prototype.extractNumbers = function(returnType){
    	var i, l = this.length, t = isNaN(returnType), r = ""
    	for (i=0; i<l; i++) if (isNaN(this.charAt(i)) == t) r += this.charAt(i);
    	return r
    }
    This one will take a string and give back either all the numbers that are in it or all the non numbers. This is based on the returnType argument. If nothing is passed for returnType, all the numbers are returned. example:

    myString = "5lh45gl5jhghj37g63ly54g89"

    myStringNums = myString.extractNumbers(1) // a number passed, traces 545537635489
    myStringChars = myString.extractNumbers("a") // a nonNumber passed, traces lhgljhghjglyg
    trace(myStringNums)
    trace(myStringChars)

    myStringNums2 = myString.extractNumbers() // default, nothing passed, traces 545537635489
    trace(myStringNums2)

    Im posting it on layer51 proto as Im posting this

  6. #6
    Senior Member
    Join Date
    Oct 2002
    Posts
    273
    Thanks for the help. I think I've got a grip on the string fnctions now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center