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. PWDX查找程序执行路径

    PWDX通过PID号查找文件对应的启动目录 在linux 64位 5.4及SunOS 5.10上测试通过 通常的做法: [root@app1 bin]# ps -ef | grep java root ...

  2. (转载)Redis5.0重量级特性Stream尝鲜

    转 导读:Redis5.0最新重点推出了Stream的支持,给众多架构师在消息队列方面带来了新的选择,特别是Redis粉丝们绝对是一个福音.那么Redis的Stream有哪些特别的功能?跟kafka有 ...

  3. CentOS 7 设置静态IP

    cd /etc/sysconfig/network-scripts/ sudo vi ifcfg-eno16777736 BOOTPROTO=static #dhcp改为static(修改) IPAD ...

  4. Android中Sqlite数据库多线程并发问题

    最近在做一个Android项目, 为了改善用户体验,把原先必须让用户“等待”的过程改成在新线程中异步执行.但是这样做遇到了多个线程同时需要写Sqlite数据库,导致操作数据库失败. 本人对Java并不 ...

  5. win 停止tomcat

    1.首先查找到占用8080端口的进程号PID是多少 CMD>netstat -ano | findstr 8080 这个命令输出的最后一列表示占用8080端口的进程号是多少,假设为1234 2. ...

  6. [UFLDL] Basic Concept

    博客内容取材于:http://www.cnblogs.com/tornadomeet/archive/2012/06/24/2560261.html 参考资料: UFLDL wiki UFLDL St ...

  7. Eclipse设置默认的换行长度

    1. 点击Window->Preferences->Java->Code Style->Formatter 2. 点击New,给profile随意取个名字,点击OK 3. Ma ...

  8. HTML 选择目录

    <input type="file" webkitdirectory directory multiple/>

  9. 服务器虚拟化ESXi 5.5安装过程

    研究服务器虚拟化实践小结: 实验服务器硬件: 主板 华硕P8B-C/2L CPU Intel Xeon E3-1230 V2 3.3GHz RAM 8G ECC 1600MHz 硬盘 2T 2块 软件 ...

  10. cocos2dx内存管理

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