字符串

1.反义字符及特殊字符
代码输出
\’单引号
\"双引号
\\反斜杠
\n换行
\r回车
\ttab(制表符)
\b退格符
\f换页符
2.字符串方法
方法名return
charAt()返回指定索引位置的字符
charCodeAt()返回指定索引位置字符的 Unicode 值
concat()连接两个或多个字符串,返回连接后的字符串
fromCharCode()将 Unicode 转换为字符串
indexOf()返回字符串中检索指定字符第一次出现的位置
lastIndexOf()返回字符串中检索指定字符最后一次出现的位置
localeCompare()用本地特定的顺序来比较两个字符串
match()找到一个或多个正则表达式的匹配
replace()替换与正则表达式匹配的子串
search()检索与正则表达式相匹配的值
slice()提取字符串的片断,并在新的字符串中返回被提取的部分
split()把字符串分割为子字符串数组
substr()从起始索引号提取字符串中指定数目的字符
substring()提取字符串中两个指定的索引号之间的字符
toLocaleLowerCase()根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射
toLocaleUpperCase()根据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射
toLowerCase()把字符串转换为小写
toString()返回字符串对象值
toUpperCase()把字符串转换为大写
trim()移除字符串首尾空白
valueOf()返回某个字符串对象的原始值
charAt() 返回索引位置的字符
var str = "hello world"
var str1 = str.charAt(4)
console.log(str)    //hello world
console.log(str1)   //o
charCodeAt()返回指定索引位置字符的Unicode
var str = "hello world"
var str1 = str.charCodeAt(4)
console.log(str)    //hello world
console.log(str1)   //111 即o的Unicode值
concat()连接两个或多个字符串,返回连接后的字符串
var str1 = "hello";
var str2 = "world";
var str3 = str1.concat(str2)
console.log(str1) //hello
console.log(str2) //world
console.log(str3) //helloworld
fromCharCode()将 Unicode 转换为字符串
var str =String.fromCharCode(72,69,76,76,79);
console.log(str) //HELLO
indexOf() 返回字符串中检索指定字符第一次出现的位置
var str = "hello world"
var str1 = str.indexOf("o")
var str2 = str.indexOf("o",6)
console.log(str)    //hello world
console.log(str1)   //4
console.log(str2)   //7 从开始位置向后找到的字符在字符串中的位置下标
lastIndexOf() 返回字符串中检索指定字符最后一次出现的位置
var str = "hello world"
var str1 = str.lastIndexOf("o")
var str2 = str.lastIndexOf("o",7)
console.log(str)    //hello world
console.log(str1)   //7 从后向前查找,找到的第一个字符的下标位置
console.log(str2)   //7 从第7个位置开始从后向前查找的字符的下标位置即字符串hello wo从后向前查找到的元素的下标位置
localeCompare()方法返回一个数字来指示一个参考字符串是否在排序顺序前面或之后或与给定字符串相同。
var str1 = "a";
var str2 = "c";
var str3 = "cba";
var str4 = "abc"
console.log(str1.localeCompare(str2)) //-1
console.log(str3.localeCompare(str4)) //1
console.log(str1.localeCompare(str1)) //0
console.log(str1.localeCompare(str4)) //-1
match()方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配,有则返回数组,没有则返回null
var str1="The rain in SPAIN stays mainly in the plain";
var str2 = str1.match(/ain/gi)// 全局查找字符串 "ain",且不区分大小写
var str3 = str1.match(/hello/gi)
console.log(str1) // The rain in SPAIN stays mainly in the plain
console.log(str2) // ["ain", "AIN", "ain", "ain"] 返回由元素组成的数组
console.log(str3) // null
replace()替换与正则表达式匹配的子串
var str1="Mr Blue has a blue house and a blue car"; 
var str2 = str1.replace(/blue/gim, "red");// g执行全局匹配(查找所有匹配而非在找到第一个匹配后停止),i执行对大小写不敏感的匹配,m执行多行匹配。
var str3 = str1.replace(/hello/gim,"red")
console.log(str1) // Mr Blue has a blue house and a blue car
console.log(str2) // Mr red has a red house and a red car
console.log(str3) // Mr Blue has a blue house and a blue car
search()检索与正则表达式相匹配的值返回匹配到的下标
var str1="Mr Blue has a blue house and a blue car"; 
var str2 = str1.search(/blue/gim, "red");// g执行全局匹配(查找所有匹配而非在找到第一个匹配后停止),i执行对大小写不敏感的匹配,m执行多行匹配。
var str3 = str1.search(/hello/gim,"red")
console.log(str1) // Mr Blue has a blue house and a blue car
console.log(str2) //3
console.log(str3) //-1
slice()提取字符串的片断,并在新的字符串中返回被提取的部分,包括开始下标,不包含结束下标
var str = "hello world"
var str1 = str.slice(3)
var str2 = str.slice(3,8)
console.log(str)    //hello world
console.log(str1)   //lo world
console.log(str2)   //lo wo
split()把字符串通过某个字符分割为子字符串数组
var str = "How are you doing today"
var str1 = str.split(" ")
console.log(str)    //How are you doing today
console.log(str1)   //["How", "are", "you", "doing", "today"]
substr()从起始索引号提取字符串中指定数目的字符,从开始下标截取n个字符
var str = "hello world"
var str1 = str.substr(3)
var str2 = str.substr(3,4)
console.log(str)    //hello world
console.log(str1)   //lo world
console.log(str2)   //lo w
substring()提取字符串中两个指定的索引号之间的字符,返回的子串包括开始处的字符,但不包括结束处的字符。
var str = "hello world"
var str1 = str.substring(3)
var str2 = str.substring(3,8)
console.log(str)    //hello world
console.log(str1)   //lo world
console.log(str2)   //lo wo
toLocaleLowerCase()根据主机的语言环境把字符串转换为小写,只有几种语言(如土耳其语)具有地方特有的大小写映射
var str="Hello World!"
var str1 = str.toLocaleLowerCase()
console.log(str)    //Hello World!
console.log(str1)   //hello world!
toLocaleUpperCase()根据主机的语言环境把字符串转换为大写,只有几种语言(如土耳其语)具有地方特有的大小写映射
var str="hello world!"
var str1 = str.toLocaleUpperCase()
console.log(str)    //hello world!
console.log(str1)   //HELLO WORLD!
toLowerCase()把字符串转换为小写
var str="Hello World!"
var str1 = str.toLowerCase()
console.log(str)    //Hello World!
console.log(str1)   //hello world!
toUpperCase()把字符串转换为大写
var str="Hello World!"
var str1 = str.toUpperCase()
console.log(str)    //Hello World!
console.log(str1)   //HELLO WORLD!
toString()

返回字符串对象值,可将其他类型转化为字符串类型

//转换进制
var num = 15;
var str1 = num.toString();
var str2 = num.toString(2);
var str3 = num.toString(8);
var str4 = num.toString(16)
console.log(str1)   //15
console.log(str2)   //1111
console.log(str3)   //17
console.log(str4)   //f
trim()移除字符串首尾空白
var str= "  Hello World!   "
var str1 = str.trim()
console.log(str)    //  Hello World!   
console.log(str1)   //Hello World!
valueOf()返回某个字符串对象的原始值
var str= "Hello World!"
var str1 = str.valueOf()
console.log(str)    //Hello World!   
console.log(str1)   //Hello World!

参考:http://www.runoob.com/js/js-strings.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值