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 ...
随机推荐
- android 自定义无限循环播放的viewPager。轮播ViewPager。实现循环播放 广告,主题内容,活动,新闻内容时。
前言 实际项目需要一个 播放广告的控件,可能有多个广告图片.每个一段时间更换该图片.简单来说,就是一个 “循环播放图片”的控件. 间隔时间更换图片 一般来说,图片切换时需要有动画效果 需要支持手势,用 ...
- CentOS 添加 Oracle YUM 源
最新文章:Virson's Blog 文章来自:Oracle 官方 YUM 源 Introduction The Oracle public yum server offers a free and ...
- portfolio
1.工作量计算逻辑: 原始待办事项: 预估2个冲刺,如下图所示: Sprint1的故事点计划工作量5,空闲工作量28.如下图 Sprint2为预估冲刺,指的是预估待办事项在后续冲刺的预估计划,后续冲刺 ...
- Spark学习笔记——泰坦尼克生还预测
package kaggle import org.apache.spark.SparkContext import org.apache.spark.SparkConf import org.apa ...
- bootstrap-select 多选下拉框使用教程
http://silviomoreto.github.io/bootstrap-select/ 一.使用bootstrap-select组件时,先引用下列文件 最后一个文件 defaults-zh_C ...
- mybatis xml 文件中like模糊查询
1.直接传参法 直接传参法,就是将要查询的关键字keyword,在代码中拼接好要查询的格式,如%keyword%,然后直接作为参数传入mapper.xml的映射文件中. 2.CONCAT()函数 My ...
- [Algorithm] Maximum Flow
Ref MIT: lecture-13-incremental-improvement-max-flow-min-cut/ Ford Fulkerson algorithm for finding m ...
- SpringBoot------Servlet3.0的注解自定义原生Listener监听器
前言 常用监听器: //contextListener可以监听数据库的连接,第三方组件的交互,还有静态文件加载等等 servletContextListener HttpSessionListener ...
- SpringBoot自定义错误页面,SpringBoot 404、500错误提示页面
SpringBoot自定义错误页面,SpringBoot 404.500错误提示页面 SpringBoot 4xx.html.5xx.html错误提示页面 ====================== ...
- 看看大网站都用什么操作系统和Web服务器
以下内容为网络上转载总结,不是很准确 Google 用哪些软件做 Web Server? 除了有两个节点操作系统看出来是 Linux 外,其他的都是未知的. Web 服务器用的都是 GWS ? 我估计 ...