Finding out a characters Unicode character code
Have you ever needed to know a character’s Unicode character code and had to spend minutes (or hours) looking it up? Well, here’s a little trick I learnt recently. ActionScript 3.0 (and ActionScript 2.0) have a handy little charCodeAt()
method in the String class which takes a numeric parameter which specifies the desired character index in a string, and returns the numeric Unicode character code for that index. A poor explanation, I know, so lets take a look at some code
Not entirely interesting. Basically you define a string (in this case I just use an inline string rather than create a new temporary variable of String type) and then call the charCodeAt()
method to convert that string into a number. The charCodeAt()
method actually takes a single parameter, which, according to the documentation is:
An integer that specifies the position of a character in the string. The first character is indicated by 0, and the last character is indicated by
my_str.length - 1
.
If you don’t pass in any value, the index value defaults to zero, which is the first character in the string. Bingo!
Now that you’re all suitably underwhelmed, lets look at another example…
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" backgroundColor="white" > <mx:Script> <![CDATA[ [Bindable] private var charCode:Number = "&".charCodeAt(); ]]> </mx:Script> <mx:Label text="@ = {'@'.charCodeAt()}" /> <mx:Label text="& = {charCode}" /> </mx:Application>
|
11:25 AM
|
Labels:
Flex Controls Samples,
Flex Samples
|
This entry was posted on 11:25 AM
and is filed under
Flex Controls Samples
,
Flex Samples
.
You can follow any responses to this entry through
the RSS 2.0 feed.
You can leave a response,
or trackback from your own site.
0 comments:
Post a Comment