1.字符串方法
str.charAt() //在xx位置处字符是什么
str.toLowerCase() //全转为小写字符
str.toUpperCase() //全转为大写字符
str.indexOf() //xx字符首次出现的位置
str.laseIndexOf() //xx字符最后出现的位置
str.substring() //字符串从哪个位置截取到哪个位置,原数组不变
str.split() //字符串以xx字符分割为数组
var arr = 'If you must say yes, say it with an open heart.'; console.log(arr.charAt(3));//yconsole.log(arr.toLowerCase());//if you must say yes, say it with an open heart.console.log(arr.toUpperCase());//IF YOU MUST SAY YES, SAY IT WITH AN OPEN HEART.console.log(arr.indexOf('y'));//3console.log(arr.lastIndexOf('y'));//23console.log(arr.subString(4,10));//ou musconsole.log(arr.split(" "));//["If", "you", "must", "say", "yes,", "say", "it", "with", "an", "open", "heart."]
1.substrng(start开始位置的索引,end结束位置索引) 截取的位置不包含结束位置的字符,只写一个参数表示从开始位置截取到最后
var str='abcdefg';
str.substring(1) //得到bcdefg str.substring(1,3) //得到bc
输入负值时将负值变为0,哪个较小作为开始位置
str.substing(-1,1) =>str.substring(0,1) //a
str.substring(1,-2) =>str.substring(0,1) //a
2.slice(start开始位置索引,end结束位置索引) 基本和substring相似,区别在参数为负数。
var str='abcdefg';
str.slice(1) //bcdefg str.substring(1,3) // bc
输入负值时 值与字符串的长度相加
str.slice(-1) =>str.slice(6) //g
str.slice(1,-2) =>str.slice(1,5) //bcde
str.slice(-2,-1)=>str.slice(5,6) //f
值绝对值大于字符串的长度时变为 0
str.slice(-22) =>str.substring(0) //abcdefg
第二个参数绝对值大于字符串的长度时,返回''
3.substr(start开始位置索引,end需要返回的字符个数)
var str='abcdefg';
str.substr(1) //bcdefg str.substr(1,1) //b
输入负值时 start参数与字符串的长度相加 ,end为负时参数变为0
str.substr(-1) =>str.substr(6)//g
str.substr(-2,-3) // ''
2.数组方法
arr.push() //在数组后面添加元素,返回数组长度,原数组改变
arr.unshift() //在数组前面添加元素,返回数组长度,原数组改变
arr.pop() //删除数组最后一个元素,返回最后一个元素,原数组改变
arr.shift() //删除数组第一个元素,返回第一个元素,原数组改变
arr.join() //以xx字符把数组各元素连接成字符串,原数组不变
arr.splice(start,num,args) //从start位置起,把num个元素,换成args=a,b,c,d,e,原数组改变
arr.reverse() //反转数组,原数组改变
arr.concat() //拼接数组,原数组不变
arr.sort() //从小到大排序,原数组改变
var arr = [1,2,'three',4,5];var arr1 = ['love',99] ; console.log(arr.push(6));//6console.log(arr.unshift(0));//7console.log(arr.pop());//6console.log(arr.shift());//0console.log(arr.join('-'));//1-2-three-4-5console.log(arr.splice(2,1,3,4));//["three"]console.log(arr);//[1, 2, 3, 4, 4, 5]console.log(arr.reverse());//[5, 4, 4,3, 2, 1]console.log(arr.concat(arr1));//[5, 4, 4, 3, 2, 1, "love", 99]console.log(arr.sort());//[1, 2, 3, 4, 4, 5]
3.slice(startIndex, endIndex) //截取startIndex开始的(endIndex-startIndex)个数据,字符串数组都可以,如果endIndex为负,则相当于(endIndex+原数据长度),操作后原数据不变
var arr = [1,'two',3];var arr1 = 'love'; console.log(arr.slice(1,-1));//['two']console.log(arr.slice(1,3));//["two", 3]console.log(arr1.slice(1,3));//ov
4.数学方法
Math.random() // 0~1随机数
Math.pow(x,y) // x的y次方
Math.sqrt(x) // x开2次方
Math.abs() // 绝对值
Math.floor(x) // 少于等于x的最大整数
Math.ceil(x) // 大于等于x的最小整数
Math.round(x) // 四舍五入
Math.max(x, y, z) // 返回最大值
Math.min(x, y, z) // 返回最小值
var a = 3.4; var b = 6.6; console.log(Math.random());//0-1随机数 console.log(Math.pow(a,3));//39.3--a的3次方 console.log(Math.sqrt(a));//1.84--开2次方 console.log(Math.abs(a));//绝对值 console.log(Math.floor(a));//3--少于等于a的最大整数 console.log(Math.ceil(a));//4--大于等于a的最小整数 console.log(Math.round(a));//3--四舍五入 console.log(Math.max(a,b,1));//6.6--返回最大值 console.log(Math.min(a,b,1));//1--返回最小值
以上是总结字符串、数组及Math的常见方法的详细内容。更多信息请关注PHP中文网其他相关文章!

JavaScript核心数据类型在浏览器和Node.js中一致,但处理方式和额外类型有所不同。1)全局对象在浏览器中为window,在Node.js中为global。2)Node.js独有Buffer对象,用于处理二进制数据。3)性能和时间处理在两者间也有差异,需根据环境调整代码。

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python和JavaScript的主要区别在于类型系统和应用场景。1.Python使用动态类型,适合科学计算和数据分析。2.JavaScript采用弱类型,广泛用于前端和全栈开发。两者在异步编程和性能优化上各有优势,选择时应根据项目需求决定。

选择Python还是JavaScript取决于项目类型:1)数据科学和自动化任务选择Python;2)前端和全栈开发选择JavaScript。Python因其在数据处理和自动化方面的强大库而备受青睐,而JavaScript则因其在网页交互和全栈开发中的优势而不可或缺。

Python和JavaScript各有优势,选择取决于项目需求和个人偏好。1.Python易学,语法简洁,适用于数据科学和后端开发,但执行速度较慢。2.JavaScript在前端开发中无处不在,异步编程能力强,Node.js使其适用于全栈开发,但语法可能复杂且易出错。

javascriptisnotbuiltoncorc; saninterpretedlanguagethatrunsonenginesoftenwritteninc.1)javascriptwasdesignedAsalightweight,解释edganguageforwebbrowsers.2)Enginesevolvedfromsimpleterterterpretpreterterterpretertestojitcompilerers,典型地提示。

JavaScript可用于前端和后端开发。前端通过DOM操作增强用户体验,后端通过Node.js处理服务器任务。1.前端示例:改变网页文本内容。2.后端示例:创建Node.js服务器。

选择Python还是JavaScript应基于职业发展、学习曲线和生态系统:1)职业发展:Python适合数据科学和后端开发,JavaScript适合前端和全栈开发。2)学习曲线:Python语法简洁,适合初学者;JavaScript语法灵活。3)生态系统:Python有丰富的科学计算库,JavaScript有强大的前端框架。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

禅工作室 13.0.1
功能强大的PHP集成开发环境

SublimeText3 Linux新版
SublimeText3 Linux最新版

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中