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

时间:2023-01-25 09:22:31

How can I convert a JavaScript string value to be in all lower case letters?

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

Example: "Your Name" to "your name"

例如:“你的名字”到“你的名字”

10 个解决方案

#1


1487  

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

#2


388  

Use either toLowerCase or toLocaleLowerCase methods of the String object. The difference is that toLocaleLowerCase will take current locale of the user/host into account. As per § 15.5.4.17 of the ECMAScript Language Specification (ECMA-262), toLocaleLowerCase

使用对字符串对象的toLowerCase或toLocaleLowerCase方法。区别在于,toLocaleLowerCase将考虑用户/主机的当前区域设置。根据§15.5.4.17 ECMAScript语言规范(ecma - 262),toLocaleLowerCase……

…works exactly the same as toLowerCase except that its result is intended to yield the correct result for the host environment’s current locale, rather than a locale-independent result. There will only be a difference in the few cases (such as Turkish) where the rules for that language conflict with the regular Unicode case mappings.

与toLowerCase完全一样,只不过它的结果是为了给宿主环境的当前语言环境提供正确的结果,而不是一个独立于语言环境的结果。在少数情况(如土耳其)中,与常规的Unicode案例映射冲突的规则将会有所不同。

Example:

例子:

var lower = 'Your Name'.toLowerCase();

Also note that the toLowerCase and toLocaleLowerCase functions are implemented to work generically on any value type. Therefore you can invoke these functions even on non-String objects. Doing so will imply automatic conversion to a string value prior to changing the case of each character in the resulting string value. For example, you can apply toLowerCase directly on a date like this:

还要注意,toLowerCase和toLocaleLowerCase函数在任何值类型上都是通用的。因此,即使在非字符串对象上也可以调用这些函数。这样做将意味着在更改产生的字符串值中每个字符的情况之前自动转换为字符串值。例如,您可以直接将toLowerCase应用于这样的日期:

var lower = String.prototype.toLowerCase.apply(new Date());

and which is effectively equivalent to:

这相当于:

var lower = new Date().toString().toLowerCase();

The second form is generally preferred for its simplicity and readability. On earlier versions of IE, the first had the benefit that it could work with a null value. The result of applying toLowerCase or toLocaleLowerCase on null would yield null (and not an error condition).

第二种形式通常更倾向于它的简单性和可读性。在IE的早期版本中,第一个版本的好处是它可以使用空值。将toLowerCase或toLocaleLowerCase应用于null的结果将产生null(而不是一个错误条件)。

#3


23  

Yes, any string in JavaScript has a toLowerCase() method that will return a new string that is the old string in all lower case. The old string will remain unchanged.

是的,JavaScript中的任何字符串都有一个toLowerCase()方法,它将返回一个新字符串,它是所有小写字母中的旧字符串。旧字符串将保持不变。

So, you can do something like:

你可以这样做:

"Foo".toLowerCase();
document.getElementById('myField').value.toLowerCase();

#4


16  

toLocaleUpperCase() or lower case functions don't behave like they should do.

toLocaleUpperCase()或小写函数的行为不应该像它们应该做的那样。

For example in my system, Safari 4, Chrome 4 Beta, Firefox 3.5.x it converts strings with Turkish characters incorrectly.

例如在我的系统中,Safari 4, Chrome 4 Beta, Firefox 3.5。它不正确地将字符串转换为土耳其字符。

The browsers respond to navigator.language as "en-US", "tr", "en-US" respectively.

浏览器响应导航器。语言为“en-US”、“tr”、“en-US”。

But there is no way to get user's Accept-Lang setting in the browser as far as I could find.

但是,在我所能找到的浏览器中,没有办法让用户的Accept-Lang设置。

Only Chrome gives me trouble although I have configured every browser as tr-TR locale preferred.

只有Chrome会给我带来麻烦,虽然我已经把每个浏览器都配置为tr-TR区域。

I think these settings only affect HTTP header, but we can't access to these settings via JS.

我认为这些设置只影响HTTP头,但是我们不能通过JS访问这些设置。

In the Mozilla documentation it says "The characters within a string are converted to ... while respecting the current locale.

在Mozilla文档中,它说“字符串中的字符被转换为……”在尊重当前语言环境的同时。

For most languages, this will return the same as ...".

对于大多数语言,这将返回相同的……

I think it's valid for Turkish, it doesn't differ it's configured as en or tr.

我认为它对土耳其人来说是有效的,它没有被配置成en或tr。

In Turkish it should convert "DİNÇ" to "dinç" and "DINÇ" to "dınç" or vice-versa.

在土耳其,它应该将“D”NC转换为“dinc”,“dinc”改为“D NC”,反之亦然。

#5


11  

example

例子

<script type="text/javascript">
var yourstring = 'Your Name'
var lowercase = yourstring.toLowerCase();
document.write('original string:<b> ' + yourstring + '</b><br>');
document.write('converted sting <b>' + lowercase + '</b>');
</script>

try it on

试穿一下

http://htmledit.squarefree.com/

#6


9  

I payed attention that lots of people are looking for strtolower() in JavaScript. They are expecting the same function name as in other languages, that's why this post is here.

我注意到很多人都在JavaScript中寻找strtolower()。他们期望的函数名和其他语言一样,这就是为什么这篇文章在这里。

I would recommend using native Javascript function

我建议使用原生Javascript函数。

"SomE StriNg".toLowerCase()

“一些字符串“.toLowerCase()

Here's the function that behaves exactly the same as PHP's one (for those who are porting PHP code into js)

这里的函数与PHP的功能完全相同(对于那些将PHP代码移植到js中的人)

function strToLower (str) {
    return String(str).toLowerCase();
}

#7


6  

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.

例如,我正在使用一个插件,并且很困惑为什么我得到了一个“扩展”。tolowercase不是一个函数“JS错误”。

 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!

产生了错误“扩展”。toLowerCase不是一个函数“所以我尝试了这段代码,它揭示了问题!

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!:

输出是“(typeof扩展)=>对象<=”,因此,我没有得到输入的字符串var。这个修复是直接的,只是把这个该死的东西变成一个字符串!

var extension = String(extension);

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

在cast之后,extension.toLowerCase()函数运行良好。

#8


6  

Method or Function: toLowerCase(), toUpperCase()

方法或函数: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".

描述:这些方法用来覆盖从小写字母到大写字母的字符串或字母表,反之亦然。e。旅客:"和" "和"。

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.

#9


1  

Opt 1 : using toLowerCase();

选择1:使用toLowerCase();

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

Opt 2 : Using your own function

选择2:使用你自己的功能。

 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);

#10


-3  

Try

试一试

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

Demo - JSFiddle

演示——JSFiddle

#1


1487  

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

#2


388  

Use either toLowerCase or toLocaleLowerCase methods of the String object. The difference is that toLocaleLowerCase will take current locale of the user/host into account. As per § 15.5.4.17 of the ECMAScript Language Specification (ECMA-262), toLocaleLowerCase

使用对字符串对象的toLowerCase或toLocaleLowerCase方法。区别在于,toLocaleLowerCase将考虑用户/主机的当前区域设置。根据§15.5.4.17 ECMAScript语言规范(ecma - 262),toLocaleLowerCase……

…works exactly the same as toLowerCase except that its result is intended to yield the correct result for the host environment’s current locale, rather than a locale-independent result. There will only be a difference in the few cases (such as Turkish) where the rules for that language conflict with the regular Unicode case mappings.

与toLowerCase完全一样,只不过它的结果是为了给宿主环境的当前语言环境提供正确的结果,而不是一个独立于语言环境的结果。在少数情况(如土耳其)中,与常规的Unicode案例映射冲突的规则将会有所不同。

Example:

例子:

var lower = 'Your Name'.toLowerCase();

Also note that the toLowerCase and toLocaleLowerCase functions are implemented to work generically on any value type. Therefore you can invoke these functions even on non-String objects. Doing so will imply automatic conversion to a string value prior to changing the case of each character in the resulting string value. For example, you can apply toLowerCase directly on a date like this:

还要注意,toLowerCase和toLocaleLowerCase函数在任何值类型上都是通用的。因此,即使在非字符串对象上也可以调用这些函数。这样做将意味着在更改产生的字符串值中每个字符的情况之前自动转换为字符串值。例如,您可以直接将toLowerCase应用于这样的日期:

var lower = String.prototype.toLowerCase.apply(new Date());

and which is effectively equivalent to:

这相当于:

var lower = new Date().toString().toLowerCase();

The second form is generally preferred for its simplicity and readability. On earlier versions of IE, the first had the benefit that it could work with a null value. The result of applying toLowerCase or toLocaleLowerCase on null would yield null (and not an error condition).

第二种形式通常更倾向于它的简单性和可读性。在IE的早期版本中,第一个版本的好处是它可以使用空值。将toLowerCase或toLocaleLowerCase应用于null的结果将产生null(而不是一个错误条件)。

#3


23  

Yes, any string in JavaScript has a toLowerCase() method that will return a new string that is the old string in all lower case. The old string will remain unchanged.

是的,JavaScript中的任何字符串都有一个toLowerCase()方法,它将返回一个新字符串,它是所有小写字母中的旧字符串。旧字符串将保持不变。

So, you can do something like:

你可以这样做:

"Foo".toLowerCase();
document.getElementById('myField').value.toLowerCase();

#4


16  

toLocaleUpperCase() or lower case functions don't behave like they should do.

toLocaleUpperCase()或小写函数的行为不应该像它们应该做的那样。

For example in my system, Safari 4, Chrome 4 Beta, Firefox 3.5.x it converts strings with Turkish characters incorrectly.

例如在我的系统中,Safari 4, Chrome 4 Beta, Firefox 3.5。它不正确地将字符串转换为土耳其字符。

The browsers respond to navigator.language as "en-US", "tr", "en-US" respectively.

浏览器响应导航器。语言为“en-US”、“tr”、“en-US”。

But there is no way to get user's Accept-Lang setting in the browser as far as I could find.

但是,在我所能找到的浏览器中,没有办法让用户的Accept-Lang设置。

Only Chrome gives me trouble although I have configured every browser as tr-TR locale preferred.

只有Chrome会给我带来麻烦,虽然我已经把每个浏览器都配置为tr-TR区域。

I think these settings only affect HTTP header, but we can't access to these settings via JS.

我认为这些设置只影响HTTP头,但是我们不能通过JS访问这些设置。

In the Mozilla documentation it says "The characters within a string are converted to ... while respecting the current locale.

在Mozilla文档中,它说“字符串中的字符被转换为……”在尊重当前语言环境的同时。

For most languages, this will return the same as ...".

对于大多数语言,这将返回相同的……

I think it's valid for Turkish, it doesn't differ it's configured as en or tr.

我认为它对土耳其人来说是有效的,它没有被配置成en或tr。

In Turkish it should convert "DİNÇ" to "dinç" and "DINÇ" to "dınç" or vice-versa.

在土耳其,它应该将“D”NC转换为“dinc”,“dinc”改为“D NC”,反之亦然。

#5


11  

example

例子

<script type="text/javascript">
var yourstring = 'Your Name'
var lowercase = yourstring.toLowerCase();
document.write('original string:<b> ' + yourstring + '</b><br>');
document.write('converted sting <b>' + lowercase + '</b>');
</script>

try it on

试穿一下

http://htmledit.squarefree.com/

#6


9  

I payed attention that lots of people are looking for strtolower() in JavaScript. They are expecting the same function name as in other languages, that's why this post is here.

我注意到很多人都在JavaScript中寻找strtolower()。他们期望的函数名和其他语言一样,这就是为什么这篇文章在这里。

I would recommend using native Javascript function

我建议使用原生Javascript函数。

"SomE StriNg".toLowerCase()

“一些字符串“.toLowerCase()

Here's the function that behaves exactly the same as PHP's one (for those who are porting PHP code into js)

这里的函数与PHP的功能完全相同(对于那些将PHP代码移植到js中的人)

function strToLower (str) {
    return String(str).toLowerCase();
}

#7


6  

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.

例如,我正在使用一个插件,并且很困惑为什么我得到了一个“扩展”。tolowercase不是一个函数“JS错误”。

 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!

产生了错误“扩展”。toLowerCase不是一个函数“所以我尝试了这段代码,它揭示了问题!

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!:

输出是“(typeof扩展)=>对象<=”,因此,我没有得到输入的字符串var。这个修复是直接的,只是把这个该死的东西变成一个字符串!

var extension = String(extension);

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

在cast之后,extension.toLowerCase()函数运行良好。

#8


6  

Method or Function: toLowerCase(), toUpperCase()

方法或函数: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".

描述:这些方法用来覆盖从小写字母到大写字母的字符串或字母表,反之亦然。e。旅客:"和" "和"。

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.

#9


1  

Opt 1 : using toLowerCase();

选择1:使用toLowerCase();

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

Opt 2 : Using your own function

选择2:使用你自己的功能。

 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);

#10


-3  

Try

试一试

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

Demo - JSFiddle

演示——JSFiddle