ARTICLE DETAIL

资讯详情

深耕编程入门与网站建设的一线实战洞察。

javascript练习之string

javascript练习之string 练习1将字符串welome to javascript所有首字母转为大写第一步声明字符串第二步将字符串转化为数组第三步将每个单词头一个字母转为大写let str welome to javascript //将字符串转为数组 let arrstr.split( ) //遍历数组将每个元素的首字母转为大写 for(let i0;iarr.length;i) { let carr[i].charAt(0) arr[i]arr[i].replace(c,c.toUpperCase()) } //将数组转换为字符串 strarr.join( ) console.log(str)练习2练习2将字符串abc Def World Hello 转为HELLO-WORLD-DEF-ABClet str2abc Def World Hello //先转为大写 str2str2.toUpperCase() //将字符串分隔转为数组 str2str2.split( ) //将字符串数组反转 str2str2.reverse() //将字符串各元素进行拼接 str2str2.join(-) console.log(str2)练习3将字符串get-element-by-id 转为 getElementByIdlet str3get-element-by-id //将字符串转为字符串数组 let arr2str3.split(-) for(let i1;iarr2.length;i) { let carr2[i].charAt(0) arr2[i]arr2[i].replace(c,c.toUpperCase()) } str3arr2.join() console.log(str3)练习4通过原型为string 添加新方法getnumcount(),返回字符串中数字的个数String.prototype.getnumcount function() { let result this.match(/\d/g) return result.length } let str4hello520ddd console.log(str4.getnumcount())
返回列表