Javascript 如何对字符串中的字母数字进行排序
如何仅对字符串“Hello,world”中的字母数字进行排序。排序(6)必须返回HelloJavascript 如何对字符串中的字母数字进行排序,javascript,Javascript,如何仅对字符串“Hello,world”中的字母数字进行排序。排序(6)必须返回Hello String.prototype.sort = function(num){ if (this.toString().length <= num) { return this.toString(); } return this.toString().slice(0, num) + '...'; } // var str = this.toString().slice(0,n
String.prototype.sort = function(num){
if (this.toString().length <= num) {
return this.toString();
}
return this.toString().slice(0, num) + '...';
}
// var str = this.toString().slice(0,num) + '...';
// for(var i = 0; i<str.length; i++) {
// char = str.charAt(i);
// if(char.match(/^[a-z0-9]+$/i) == null) {
// str = str.slice(0,i) + str.slice(i+1);
// }
// }
console.log('Hello, world!'.sort(6));
String.prototype.sort=函数(num){
如果(this.toString().length要截断字符串,请不要尝试实现自己的函数,只需提供此方法的开始索引和结束索引即可:
console.log('Hello,world!'.substring(0,6));
我不确定我是否理解这个问题。这是sort还是truncate?“sort”是一个错误的词。使用“truncate”这个词。听起来像是在尝试过滤?当你说“只对字符串的字母数字排序”时是什么意思?是的,它是truncate