javascript 如何在Typescript中小写字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/51443173/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:33:14 来源:igfitidea点击:
How to lower case a string in Typescript?
提问by sumit maske
I am comparing two strings and I want to lower case a string before comparing. How can I do this? This is my code:
我正在比较两个字符串,我想在比较之前小写一个字符串。我怎样才能做到这一点?这是我的代码:
this.products = response.responseData.sort(function(a,b){
if(a.product.productName < b.product.productName){
return -1;
}
if(a.product.productName > b.product.productName){
return 1;
}
return 0;
});
回答by Sh. Pavel
Just use the:
只需使用:
.toLowerCase()
method.
方法。
In your case:
在你的情况下:
if(a.product.productName.toLowerCase() < b.product.productName.toLowerCase()){
return -1;
}
回答by Lynx 242
Use Javascript's .toLowerCase().
使用 Javascript 的 .toLowerCase()。
this.products = response.responseData.sort(function(a,b){
if(a.product.productName.toLowerCase() < b.product.productName.toLowerCase()){
return -1;
}
if(a.product.productName.toLowerCase() > b.product.productName.toLowerCase()){
return 1;
}
return 0;
});

