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 ...
随机推荐
- 【Java】移动JDK路径后,修改环境变量不生效 Error: could not open `C:\Program Files\Java\jre1.8.0_131\lib\amd64\jvm.cfg'
场景: JDK原先装在C盘的,现在移动到了D盘,并在环境变量修改了%JAVA_HOME%的新路径,但是CMD中输入java后依然报错. Error: could not open `C:\Progra ...
- C++ 智能指针二
/* 智能指针shared_ptr注意点 */ #include <iostream> #include <string> #include <memory> // ...
- 对TextFile格式文件的lzo压缩建立index索引
转自:http://blog.csdn.net/yangbutao/article/details/8519572 hadoop中可以对文件进行压缩,可以采用gzip.lzo.snappy等压缩算法. ...
- Python3数字(Number)
一.数学函数 二.随机数函数 三.三角函数 四.数学常量
- Office 2007 打开时总是出现配置进度框
解决办法: cmd 打开控制台 输入命令:reg add HKCU\Software\Microsoft\Office\12.0\Word\Options /v NoReReg /t REG_DWOR ...
- [Object Tracking] Overview of algorithms for Object Tracking
From: https://www.zhihu.com/question/26493945 可以载入史册的知乎贴 目标跟踪之NIUBILITY的相关滤波 - 专注于分享目标跟踪中非常高效快速的相关滤波 ...
- [BOOK] Applied Math and Machine Learning Basics
<Deep Learning> Ian Goodfellow Yoshua Bengio Aaron Courvill 关于此书Part One重难点的个人阅读笔记. 2.7 Eigend ...
- Polygon Offset
https://www.cnblogs.com/bitzhuwei/p/polygon-offset-for-stitching-andz-fighting.html 一个大于0的offset 会把模 ...
- iOS - DNS劫持
******科普** 1.DNS劫持的危害 不知道大家有没有发现这样一个现象,在打开一些网页的时候会弹出一些与所浏览网页不相关的内容比如这样奇(se)怪(qing)的东西 图一 或者这样 图二 ...
- 【netcore基础】.NET Core使用EPPlus实现MVC API里的Excel导出功能 配置中文表头
EPPlus 用来操作excel非常方便,不用依赖微软的office包,所以推荐使用. 下面是具体步骤和代码 首先用nuget安装 EPPlus.Core 我装的版本是 1.5.4 然后就可以用 Ex ...