Home > Web Front-end > JS Tutorial > body text

JavaScript generates uppercase and lowercase letters_javascript tips

WBOY
Release: 2016-05-16 15:51:49
Original
1543 people have browsed it

Mainly use str.charCodeAt() and String.fromCharCode() methods

--》Use charCodeAt() to get the Unicode encoding of a specific character in a string.

--》fromCharCode() accepts one (or more) specified Unicode values ​​and returns the corresponding string.

//生成大写字母 A的Unicode值为65
function generateBig_1(){
  var str = [];
  for(var i=65;i<91;i++){
    str.push(String.fromCharCode(i));
  }
  return str;
}
//生成大写字母 a的Unicode值为97
function generateSmall_1(){
  var str = [];
  for(var i=97;i<123;i++){
    str.push(String.fromCharCode(i));
  }
  return str;
}
//将字符串转换成Unicode码
function toUnicode(str){
  var codes = [];
  for(var i=0;i<str.length;i++){
    codes.push(str.charCodeAt(i));
  }
  return codes;
}

function generateSmall(){
  var ch_small = 'a';
  var str_small = '';
  for(var i=0;i<26;i++){
    str_small += String.fromCharCode(ch_small.charCodeAt(0)+i);
  }
  return str_small;
}

function generateBig(){
  var ch_big = 'A';
  var str_big = '';
  for(var i=0;i<26;i++){
    str_big += String.fromCharCode(ch_big.charCodeAt(0)+i);
  }
  return str_big;
}

console.log(generateBig());
console.log(generateSmall());

console.log(toUnicode(generateBig()));
console.log(toUnicode(generateSmall()));

console.log(generateBig_1());
console.log(generateSmall_1());

Copy after login

Available results--》

Among them, fromCharCode plays a big role in converting html entity symbols

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!