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

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

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

西门JinJin2020/03/09 21:59:25

Try

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

Demo - JSFiddle

Davaid古一2020/03/09 21:59:25

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

Tony飞云2020/03/09 21:59:25

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

}

小小Jim2020/03/09 21:59:25

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

Stafan番长2020/03/09 21:59:25

Tray this short way

  document.write((a+"").toUpperCase());
逆天乐2020/03/09 21:59:25

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.
十三西里GO2020/03/09 21:59:25

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 21:59:25

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.

DavaidL2020/03/09 21:59:25
var lowerCaseName = "Your Name".toLowerCase();