javascript toUpperCase() 不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15326582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
toUpperCase() is not working
提问by Don
回答by Explosion Pills
.toUpperCase
returns the upper-cased string. It is not an in-place modifier method.
.toUpperCase
返回大写字符串。它不是就地修饰符方法。
string = string.toUpperCase();
Documentation: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/toUpperCase
文档:https: //developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/toUpperCase
回答by ssilas777
String is Immutable. Once created, a string object can not be modified.
字符串是不可变的。一旦创建,字符串对象就不能被修改。
So here toUpperCase returns a new string, This should work-
所以这里 toUpperCase 返回一个新字符串,这应该有效-
var string ="kjsdgfiIJHBVSFIU";
var newString = string.toUpperCase();
alert(newString);
回答by Teknia
Previous answers about Strings being immutable are great! Here is just another potential run-time reason for getting this error which might be hard to spot at first. It does not detract from any of the above valid answers:
以前关于字符串不可变的答案很棒!这只是导致此错误的另一个潜在运行时原因,一开始可能很难发现。它不会减损上述任何有效答案:
If the object on which the .toUpperCase()
method is invoked is not a String, then the runtime doesn't 'find' the method toUpperCase()
since that method/function only exists on String objects.
如果.toUpperCase()
调用该方法的对象不是 String,则运行时不会“找到”该方法,toUpperCase()
因为该方法/函数仅存在于 String 对象上。
e.g. console.log(variableName.toUpperCase());
例如 console.log(variableName.toUpperCase());
If variableName is of the type String, it works fine. If it is of another type (that does not have a toUpperCase()
method, it says 'toUpperCase() is not a function (since it isn't).
如果 variableName 是 String 类型,则它工作正常。如果它是另一种类型(没有toUpperCase()
方法,它会说 'toUpperCase() 不是函数(因为它不是)。
If you know it should be a string, you can cast it like this:
如果你知道它应该是一个字符串,你可以像这样转换它:
console.log(String(variableName).toUppercase());
Hope it helps.
希望能帮助到你。
回答by yorlin
var upperCase = string.toUpperCase();
console.log(upperCase);
toUpperCase doesn't transform existing string, it just returns a uppercase string.
toUpperCase 不转换现有字符串,它只返回一个大写字符串。
回答by bcoughlan
toUpperCase
returns the new string, so you must write:
toUpperCase
返回新字符串,所以你必须写:
string = string.toUpperCase();
In many languages, strings are immutable, meaning that they can not be modified once created. While this costs in efficiency, it is important for object oriented programming, because if a String passed by reference to a function was modifiable, the state of objects could be changed without the object's consent.
在许多语言中,字符串是不可变的,这意味着它们一旦创建就无法修改。虽然这会降低效率,但它对于面向对象的编程很重要,因为如果通过引用传递给函数的 String 是可修改的,则可以在未经对象同意的情况下更改对象的状态。
回答by Topguru
var string ="kjsdgfiIJHBVSFIU";
string = string.toUpperCase();
console.log(string);
回答by ganeshprasad
Convert to string
value to toUpperCase
And toLowercase
then object with method().
转换为string
值toUpperCase
并且toLowercase
然后对象,具有()的方法。
var string ="kjsdgfiIJHBVSFIU";
console.log(string.toUpperCase());