js字符串方法汇总
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():用于将一个或多个字符串拼接起来,返回拼接得到的新字符串
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字符串方法汇总的更多相关文章
- 常用js字符串方法学习总结
2016-06-15 js数组和字符串方法有很多,并且有一部分在使用的过程中有很多方法是很容易被混淆的,今天来总结一下js中数组和字符串的方法. ♦数组(Array)的方法 1.push() 和 po ...
- js字符串方法、数组方法整理
push 向数组末尾添加一项 返回值为数组的长度: pop 删除数组最后一项: unshift 向数组开头增加一项: shift 删除数组第一项: splice 删除数组中的值:1 splice(n, ...
- js字符串方法
字符串方法根据下标返回字符:str.charAt()//传入一个下标返回字符str.charCodeAt();// 传入一个下标获取编码String.formCharCode();//接受编码,编码转 ...
- JS字符串方法总结整理
//javascript字符串方法总结 1.String.charAt(n) //取得字符串中的第n个字符 2.String.charCodeAt(n) //取得字符串中第n个字符 ...
- js 字符串方法 和 数组方法总览
字符串方法 search() 方法搜索特定值的字符串,并返回匹配的位置. 相比于indexOf(),search()可以设置更强大的搜索值(正则表 ...
- js常用字符串方法汇总
concat()将两个或多个字符的文本组合起来,返回一个新的字符串. var a = "hello"; var b = ",world"; var c = a. ...
- 基于vue项目的js工具方法汇总
以下是个人过去一年在vue项目的开发过程中经常会用到的一些公共方法,在此进行汇总,方便以后及有需要的朋友查看~ let util = {}; /** * @description 日期格式化 * @p ...
- JS数组方法汇总 array数组元素的添加和删除
js数组元素的添加和删除一直比较迷惑,今天终于找到详细说明的资料了,先给个我测试的代码^-^ var arr = new Array(); arr[0] = "aaa"; arr[ ...
- js数组方法汇总
下面主要汇总一下数组的方法 数组方法: 1.检测是否为数组的方法:Array.isArrray(); var arr=[1,2,3,4,5]; var str='string'; console.lo ...
随机推荐
- 【emWin】例程十八:jpeg图片显示
说明:1.将文件拷入SD卡内即可在指定位置绘制jpeg图片文件,不必加载到储存器. 由于jpeg格式文件显示时需要进行解压缩,耗用动态内存,iCore3所有模块受emwin缓存的限制,jpeg ...
- django template if return false
如果if的参数不存在于context中就会返回false 参考:http://stackoverflow.com/questions/11107028/django-template-if-true- ...
- Java知多少(28)super关键字
super 关键字与 this 类似,this 用来表示当前类的实例,super 用来表示父类. super 可以用在子类中,通过点号(.)来获取父类的成员变量和方法.super 也可以用在子类的子类 ...
- 说说自己对RESTful API的理解
REST不是英文上的rest单词,其英文缩写为presentational State Transfer ,直译为表现状态转移,咋看起来很学术,不懂,其实不用去死抠这个词的意思.REST是一种约束和架 ...
- golang net/http 包
https://studygolang.com/articles/9467 https://www.jianshu.com/p/be3d9cdc680b 客户端: 用来发送请求, 并处理返回结果. 涉 ...
- [React] 01 - Intro: javaScript library for building user interfaces
教学视频: http://www.php.cn/code/8217.html React 教程: http://www.runoob.com/react/react-tutorial.html 本篇是 ...
- Hibernate -- Dao层 -- CURD -- 随记
根据Where 参数 查询记录总数 .拼接SQL语句 .获取Session(hibernateTemplate.getSessionFactory().getCurrentSession()),调用C ...
- OpenGL——圆公式相关变化的绘制
#include<iostream> #include <math.h> //旧版本 固定管线 #include<Windows.h> #include <G ...
- image-set实现Retina屏幕下图片显示详细介绍
支持image-set:如果你的浏览器支持image-sete,而且是普通显屏下,此时浏览器会选择image-set中的@1x背景图像: Retina屏幕下的image-set:如果你的浏览器支持im ...
- 轮滑基础(一)(前摔,葫芦步,推步,A字转弯,弓步转弯)
轮滑新手入门推荐? [柚子陪你学轮滑轮滑教学]第一集 轮滑安全 1,站: 站立:脚可以成v字,或者平行,手放膝盖或者前伸.平行站立 膝盖相距一拳头左右,两腿间距略小于肩宽.膝盖略弯,腰下压,抬头挺胸 ...