将JavaScript字符串全部转换为小写吗?

JavaScript

梅西里

2020-03-09

如何将JavaScript字符串值转换为所有小写字母?

示例:从“您的姓名”到“您的姓名”

第294篇《将JavaScript字符串全部转换为小写吗?》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

9个回答
西门JinJin 2020.03.09

Try

<input type="text" style="text-transform: uppercase">  //uppercase
<input type="text" style="text-transform: lowercase">  //lowercase

Demo - JSFiddle

Davaid古一 2020.03.09

you can use the in built .toLowerCase() method on javascript strings. ex: var x = "Hello"; x.toLowerCase();

Tony飞云 2020.03.09

In case you want to build it yourself:

function toLowerCase(string) {

let lowerCaseString = "";

for (let i = 0; i < string.length; i++) {
    //find ASCII charcode
    let charcode = string.charCodeAt(i);

    //if uppercase
    if (charcode > 64 && charcode < 97){
        //convert to lowercase
        charcode = charcode + 32
    }

    //back to char
    let lowercase = String.fromCharCode(charcode);

    //append
    lowerCaseString = lowerCaseString.concat(lowercase);
}

return lowerCaseString

}

小小Jim 2020.03.09

Simply use JS toLowerCase()
let v = "Your Name" let u = v.toLowerCase(); or
let u = "Your Name".toLowerCase();

Stafan番长 2020.03.09

Tray this short way

  document.write((a+"").toUpperCase());
逆天乐 2020.03.09

Method or Function: toLowerCase(), toUpperCase()

Description: These methods are used to cover a string or alphabet from lower case to upper case or vice versa. e.g: "and" to "AND".

Converting to Upper Case:- Example Code:-

<script language=javascript>
var ss = " testing case conversion method ";
var result = ss.toUpperCase();
document.write(result);
</script>

Result: TESTING CASE CONVERSION METHOD

Converting to Lower Case:- Example Code:

<script language=javascript>
var ss = " TESTING LOWERCASE CONVERT FUNCTION ";
var result = ss.toLowerCase();
document.write(result);
</script>

Result: testing lowercase convert function

Explanation: In the above examples,

toUpperCase() method converts any string to "UPPER" case letters.
toLowerCase() method converts any string to "lower" case letters.
十三西里GO 2020.03.09

Opt 1 : using toLowerCase();

var x = "ABC";
x=x.toLowerCase();

Opt 2 : Using your own function

 function convertToLowerCase(str) {
      var result = ''

      for (var i = 0; i < str.length; i++) {
        var code = str.charCodeAt(i)
        if (code > 64 && code < 91) {
          result += String.fromCharCode(code + 32)
        } else {
          result += str.charAt(i)
        }
      }
      return result
    }

Call it as:

x= convertToLowerCase(x);
猿蛋蛋凯 2020.03.09

Note that the function will ONLY work on STRING objects.

For instance, I was consuming a plugin, and was confused why I was getting a "extension.tolowercase is not a function" JS error.

 onChange: function(file, extension)
    {
      alert("extension.toLowerCase()=>" + extension.toLowerCase() + "<=");

Which produced the error "extension.toLowerCase is not a function" So I tried this piece of code, which revealed the problem!

alert("(typeof extension)=>" + (typeof extension) + "<=");;

The output was"(typeof extension)=>object<=" - so AhHa, I was NOT getting a string var for my input. The fix is straight forward though - just force the darn thing into a String!:

var extension = String(extension);

After the cast, the extension.toLowerCase() function worked fine.

DavaidL 2020.03.09
var lowerCaseName = "Your Name".toLowerCase();

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android