1、length方法

    var stringObject=new String("hellow world");
console.log(stringObject.length);//12

2、字符方法charAt()、charCodeAt()     指定索引查找字符

这两个方法都接收一个参数

charAt():返回给定位置的那个字符

charCodeAt():返回指定位置的字符编码

    var stringValue="hellow world";
console.log(stringValue.charAt(1));//e
console.log(stringValue.charCodeAt(1));//101

3、字符串操作方法concat()、slice()、substr()、substring()

concat():用于将一个或多个字符串拼接起来,返回拼接得到的新字符串


var stringValue="hellow world";
var result=stringValue.concat("hello");
var result2="hellow world"+"hello";
console.log(stringValue);//hellow world
console.log(result);//hellow worldhello
console.log(result2);//hellow worldhello

slice()、substr()、substring()都是截取字符串的方法,返回一个子字符串

这三个方法都接收一个或两个参数,第一个参数指定子字符串的开始位置

slice()和substring()方法的第二个参数是指定子字符串的结束位置,而substr()的第二个参数指定的则是返回的字符个数,这三个方法都不会修改字符串本身,都是返回一个子字符串

    var stringValue="hellow world";
console.log(stringValue.slice(3));//low world
console.log(stringValue.substring(3));//low world
console.log(stringValue.substr(3));//low world
console.log(stringValue.slice(3,7));//low
console.log(stringValue.substring(3,7));//low
console.log(stringValue.substr(3,7));//low wor //参数为负数的情况 都可以与length相加 继续执行
//substring()会把负值 转换为0
//slice()就是截取两个索引之间的字符
//substr() 第二个参数为负值时,就是返回0个字符串 所以返回空字符串
console.log(stringValue.slice(-3));//rld
console.log(stringValue.substring(-3));//hellow world
console.log(stringValue.substr(-3));//rld
console.log(stringValue.slice(3,-4));//low w
console.log(stringValue.substring(3,-4));//hel
console.log(stringValue.substr(3,-4));//""

4、字符串位置方法indexOf()、lastIndexOf()

都接收一个或两个参数,第一个参数是要查的字符串,第二个参数是开始搜索的索引,返回匹配到字符串的索引,不存在则返回-1

indexOf():是从前往后搜索

lastIndexOf():是从后往前搜索

    var stringValue="hellow world";
console.log(stringValue.indexOf("o"));//4
console.log(stringValue.lastIndexOf("o"));//8
console.log(stringValue.indexOf("o",6));//8
console.log(stringValue.lastIndexOf("o",6));//4

5、trim() 删除前后空格

    var stringValue="   hellow world   ";
console.log(stringValue.trim());//"hellow world"

6、大小写转换 toLowerCase()、toUpperCase()

toLowerCase():将字符串每一项转换成小写

toUpperCase():将字符串每一项转换成大写

    var stringValue="hellow WORLD";
console.log(stringValue.toLowerCase());//hellow world
console.log(stringValue.toUpperCase());//HELLOW WORLD

7、match()、search()方法 匹配方法  过多介绍

8、split()  将字符串转化为字符串数组

接收一个或两个参数,第一个参数为分隔符,按照这分隔符分隔字符串。第二个参数指定返回字符串数组的长度

    var colors="red,blue,green,yellow"
console.log(colors.split());//["red,blue,green,yellow"]
console.log(colors.split(","));//["red", "blue", "green", "yellow"]
console.log(colors.split(""));//["r", "e", "d", ",", "b", "l", "u", "e", ",", "g", "r", "e", "e", "n", ",", "y", "e", "l", "l", "o", "w"]
console.log(colors.split(",",2));//["red", "blue"]

9、string构造函数本身有一个fromCharCode()

将多个字符编码转换为字符串

console.log(String.fromCharCode(104,101,108,108,111));//hello

10、replace()   替换方法

js字符串方法汇总的更多相关文章

  1. 常用js字符串方法学习总结

    2016-06-15 js数组和字符串方法有很多,并且有一部分在使用的过程中有很多方法是很容易被混淆的,今天来总结一下js中数组和字符串的方法. ♦数组(Array)的方法 1.push() 和 po ...

  2. js字符串方法、数组方法整理

    push 向数组末尾添加一项 返回值为数组的长度: pop 删除数组最后一项: unshift 向数组开头增加一项: shift 删除数组第一项: splice 删除数组中的值:1 splice(n, ...

  3. js字符串方法

    字符串方法根据下标返回字符:str.charAt()//传入一个下标返回字符str.charCodeAt();// 传入一个下标获取编码String.formCharCode();//接受编码,编码转 ...

  4. JS字符串方法总结整理

    //javascript字符串方法总结   1.String.charAt(n)      //取得字符串中的第n个字符   2.String.charCodeAt(n)  //取得字符串中第n个字符 ...

  5. js 字符串方法 和 数组方法总览

    字符串方法        search()             方法搜索特定值的字符串,并返回匹配的位置.         相比于indexOf(),search()可以设置更强大的搜索值(正则表 ...

  6. js常用字符串方法汇总

    concat()将两个或多个字符的文本组合起来,返回一个新的字符串. var a = "hello"; var b = ",world"; var c = a. ...

  7. 基于vue项目的js工具方法汇总

    以下是个人过去一年在vue项目的开发过程中经常会用到的一些公共方法,在此进行汇总,方便以后及有需要的朋友查看~ let util = {}; /** * @description 日期格式化 * @p ...

  8. JS数组方法汇总 array数组元素的添加和删除

    js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了,先给个我测试的代码^-^ var arr = new Array(); arr[0] = "aaa"; arr[ ...

  9. js数组方法汇总

    下面主要汇总一下数组的方法 数组方法: 1.检测是否为数组的方法:Array.isArrray(); var arr=[1,2,3,4,5]; var str='string'; console.lo ...

随机推荐

  1. ipv6禁用导致rpcbind服务启动失败实例

    ipv6禁用导致rpcbind服务启动失败实例     昨天在做服务器磁盘分区扩容的时候出现过一个服务启动的问题,在此记录.情景再现:前天晚上申请做磁盘扩容,得到批准后,昨天早上5点开始做停机调整维护 ...

  2. 概率霍夫变换(Progressive Probabilistic Hough Transform)原理详解

    概率霍夫变换(Progressive Probabilistic Hough Transform)的原理很简单,如下所述: 1.随机获取边缘图像上的前景点,映射到极坐标系画曲线: 2.当极坐标系里面有 ...

  3. 第三百九十八节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5系统环境设置

    第三百九十八节,Django+Xadmin打造上线标准的在线教育平台—生产环境部署CentOS6.5系统环境设置 1.Linux安装配置 注意事项: 虚拟机网卡桥接模式 不要拨VPN 如果,网络怎么都 ...

  4. (转)java 中unsigned类型的转换

    转自:http://blog.sina.com.cn/s/blog_77bf45a90101dld9.html 在Java中,不存在Unsigned无符号数据类型,但可以轻而易举的完成Unsigned ...

  5. Linux系统排查4——网络篇

    用于排查Linux系统的网络故障. 网络排查一般是有一定的思路和顺序的,其实排查的思路就是根据具体的问题逐段排除故障可能发生的地方,最终确定问题. 所以首先要问一问,网络问题是什么,是不通,还是慢? ...

  6. ABBYY FineReader Pro for Mac有哪些特性(下)

    使用ABBYY FineReader Pro for Mac轻松转换纸质文档.PDF文件和数字文本照片为可编辑和可搜索的文件,再也不需要手动重新输入或格式化了,相反,可以编辑.搜索.共享.归档和复制文 ...

  7. MTK 自定义系统服务

    添加系统服务需要添加aidl,service,manager文件,需要修改SystemServer,Context,ContextImpl 下面举例进行说明,主要添加一个服务,用于获取系统时间和版本号 ...

  8. nodejs 单线程 高并发

    nodejs为什么是单线程且支持高并发的脚本语言呢? 1.node的优点:I/O密集型处理(node的I/O请求都是异步的,如:sql查询.文件流操作.http请求……):异步I/O?顾名思义就是异步 ...

  9. cocos2dx内存管理

    cocos2dx基于引用计数管理内存,所有继承自CCObject的对象都将获得引用计数的能力,可通过调用retain成员函数用于引用计数值,调用release减少引用计数值,当计数值减为0时销毁对象. ...

  10. day_5.26python面试重点

    列表生成式: ''' 2018-5-26 19:40:58 生成器(generator):在Python中,这种⼀边循环⼀边计算的机制. ''' # 第一种实现 :只要把⼀个列表⽣成式的[ ]改成( ...