js letters digital conversion, digital conversion letter

Converted to the corresponding capital letters 01234

var str = 'ABC'
function options(item){
	let num = ''
	for(let i = 0; i<item.length;++i){
		num+=item.charCodeAt(i)-65
	}
	return num
}
var str1 = options(str)
console.log(str1)//012

Unicode encoding the charCodeAt () method returns the position of the specified character. The return value is 0 - integer between 65535. AZ and the corresponding Unicode encoding is 65-90, and here I subtract 65, corresponding exactly the answer I wanted (az correspondence 97-122).
Of course, corresponding to the 01234 can also be converted into the corresponding uppercase

var str = '123'
function options(item){
	let num = ''
	for(let i = 0; i<item.length;++i){
		num+=String.fromCharCode(Number(str[i])+65)
	}
	return num
}
var str1 = options(str)
console.log(str1)//BCD

the fromCharCode () accepts a specified Unicode values and returns a string. I have here the corresponding number from 0 began to count, so the results obtained are BCD.
Supplementary:
toLowerCase () method is used to all uppercase characters have all been converted to lowercase characters.
toUpperCase () method is used to convert a string to upper case.

Published 17 original articles · won praise 16 · views 6662

Guess you like

Origin blog.csdn.net/qq_45074127/article/details/104554339