Reputation: 2611
The following trick enables you to get the index of the letter in the alphabet:
let index = "x".charCodeAt() - 97;
However, that only works if your alphabet is English. Is there any way to do this for any random language?
Thanks.
Upvotes: 0
Views: 450
Reputation: 131
You can do something similar. charCodeAt()
gets the index of a unicode character in UTF-16 encoding.
This means that you would be able to use this to get the index of any unicode character between 0 and 0xFFFF. Really if you want to do this for any language you should use codePointAt()
because this lets you access all of unicode. As long as you are able to map between a letter's index in unicode and that letter's 'alphabet' you can do the 'trick' you described above.
For english this is particularly easy because unicode has all english letters stored in alphabetical order starting at index 97, therefore to get from unicode index to english alphabet index you just take away 97 from the unicode index. (however this only works for lower case letters)
For other languages this is less simple because unicode is less likely to store all their characters in 'alphabetical order' also some languages do not have an 'alphabetical order'.
How hard this would be really is going to depend on the specific language that you want to do this for.
Suggest you read:
Upvotes: 1
Reputation: 59
No, only the alphabets present in the ASCII table can extracted with such functions.
Upvotes: 3