js中字符串的常见方法

JavaScript09

js中字符串的常见方法,第1张

首先创建一个字符串,创建字符串的方法有两种:

1.字面量:var str = "abc123efg456"

2.构造函数:var str = newString("abc123efg456")

其次,需要注意的是,javascript的字符串是不可变的,String类定义的方法都不可以改变字符串内容。若是需要通过方法改变字符串的内容,需要将返回值重新赋值给原来的字符串。如:str = str.toUpperCase()

下面,介绍比较常用的字符串的方法:

var str = "1abc123abc456abc"

功能:返回指定的字符串值在字符串中首次出现的位置。

参数:s为要检索的字符,必需n为可选的字符参数,规定字符串检索的位置。

demo:

var index1 = str.indexOf("a")

var index2 =str.indexOf("a",8)

var index3 =str.indexOf("a",14)

console.log(index1)// 1

console.log(index2)// 13

console.log(index3)// -1  从检索的位置检索不到字符,返回-1

功能:返回一个指定的字符串值最后出现的位置,在一个字符串中的指定位置从后向前搜索。

参数:s为要检索的字符,必需n为可选的字符参数,规定字符串检索的位置。

demo:

var index1 =str.lastIndexOf("a") 

var index2 =str.lastIndexOf("a",11)

var index3 =str.lastIndexOf("a",0)

console.log(index1)// 13

console.log(index2)// 7

console.log(index3)// -1  从检索的位置检索不到字符,返回-1

功能:返回指定位置的字符。

参数:n表示字符串中某个位置的索引,不写默认为第0个字符。

demo:

var s = str.charAt(3)

console.log(s)  // c

功能:返回指定位置的字符的Unicode编码。

参数:n表示字符串中某个位置的索引,可以没有参数,默认是索引为0的参数。

demo:

var s = str.charCodeAt(3)

console.log(s)  // 99

注:c的unicode编码为99。

注意:charAt(n)方法和charCodeAt(n)方法类似,前者返回的是字符,后者返回的是字符的unicode编码。

功能:将编码转成字符方法。

参数:十进制和十六进制,不支持unicode编码。

demo:

var s1 =String.fromCharCode("0x56fd")

var s2 = String.fromCharCode(22269)

console.log(s1)   //国

console.log(s2)  //国

注:“国”的十六进制编码是56fd,十进制是22269。

功能:根据指定位置,截取子串,从m到n,不包括n。

参数:m,n为索引。

demo:

var s = str.slice(2,7)

console.log(s)// bc123

功能:根据指定位置,截取子串,从m到n,不包括n。

参数:m,n为索引。

demo:

var s = str.substring(2,7)

console.log(s)// bc123

功能:根据指定位置,截取子串,从m位置取,共取n个。

参数:m,n为索引。

demo:

var s = str.substr(2,7)

console.log(s)// bc123ab

功能:字符串替换。

参数:oldstr为需要被替换的字符,newstr为替换的字符。

demo:

var s =str.replace("a","l")

console.log(s)// 1lbc123abc456abc

注意:每次只能替换一个符合条件的字符。

功能:分割字符,返回成为数组。

参数:"s"为分割的字符。

demo1:

var arr1 = str.split("b")

console.log(arr1)  // ["1a", "c123a","c456a", "c"]

demo2:

var arr2 = str.split()//["1abc123abc456abc"]

console.log(arr2)

demo3:

var arr3 = str.split("")

console.log(arr3)// ["1","a", "b", "c", "1", "2","3", "a", "b", "c", "4","5", "6", "a", "b", "c"]

注意:参数不写的时候,返回原字符串组成的数组参数为空字符的时候,将每个字符分割,返回一个数组

功能:检索字符串中指定的子字符串,返回第一个与字符串匹配的索引值。

参数:需要被检索的字符串。

demo:

var s1= str.search("abc")

var s2 = str.search("efg")

console.log(s1)// 1

console.log(s2)// -1

注:如果没有找到任何匹配的子串,则返回-1。

功能:在字符串内检索指定的值,匹配到就返回指定的字符值。

参数:需要被检索的字符串。

demo:

var s1= str.match("abc")

var s2 = str.match("efg")

console.log(s1)// ["abc",index: 1, input: "1abc123abc456abc", groups: undefined]

console.log(s2)// null

注:该方法类似indexOf()和lastIndexOf(),但是它返回指定的值,没有就返回null

功能:连接两个或多个字符串,返回连接后的字符串。

参数:需要连接的字符串。

demo:

var str1="abc "

var str2="123"

console.log(str1.concat(str2))   // "abc 123"

提示:如果需要拼接字符串,用运算符“+”更简单。

功能:把字符串转换为小写。

参数:无

demo:

var str = "ABC abc"

console.log(str.toLowerCase())// abcabc

功能:把字符串转换为大写。

参数:无

demo:

var str = "ABC abc"

console.log(str.toUpperCase())// ABCABC

功能:把字符串显示为大号字体。

参数:无

demo:

var s = str.big()

document.write(str+"")

document.write(s)

功能:把字符串显示为小号字。

参数:无

demo:

var s = str.small()

document.write(str+"")

document.write(s)

功能:把字符串显示为粗体。

参数:无

demo:

var s = str.bold()

document.write(str+"")

document.write(s)

功能:把字符串显示为斜体。

参数:无

demo:

var s = str.italics()

document.write(str+"")

document.write(s)

功能:将字符以指定的颜色显示。

参数:颜色名(red)、RGB 值(rgb(255,0,0))或者十六进制数(#FF0000)。

demo:

var s = str.fontcolor("red")

document.write(str+"")

document.write(s)

功能:将字符以指定的字号显示。

参数:size参数必须是从1和7之间的数字,1表示小的字号,7表示最大的字号。

demo:

var s = str.fontsize(7)

document.write(str+"")

document.write(s)

功能:把字符串显示为超链接。

参数:链接的URL。

demo:

var s =str.link("http://www.baidu.com")

document.write(str+"")

document.write(s)

功能:把字符串加字符串显示。

参数:无

demo:

var s = str.strike()

document.write(str+"")

document.write(s)

以上是对字符串方法的整理,如有修改、补充,后续会更新。

文中如果纰漏,错误,不合理,描述不清晰,不准确等问题,欢迎大家留言指正...

字符串的属性:length =》 代表字符串长度

字符串中,字符的位置也是从0开始,依次递增。

不会修改原字符串,会将拼接好的字符串以返回值的方式返回出来。

可以在字符串中查找指定的字符。

可以查到的的话返回值字符所在的位置。

查不到返回 -1。

如果要查找的字符在字符中存在多个,永远只找一个。

使用方法和 indexOf 一样。

但是 indexOf 是从前往后查找,lastindexOf() 是从后往前查找。

有两个参数。

第一个参数,旧的字符。

第二个参数,新的字符。

不会修改原数组。

会将替换好的数组以返回值的形式返回出来。

如果旧的字符在字符串中不止一个,则替换第一个。

不会改变原数组。

会将转化好的数组以返回值的形式返回出来 。

如果括号中什么都不写;则将整个字符串作为数组的一个元素转化成数组。

如果括号中是空字符串,则将每个字符都作为一个数组的元素转化成数组。

如果括号中写字符,则将这个字符变成逗号(,),并以其隔开数组元素。

如果字符串中没有这个字符,效果相当于什么都不写。

如果目标字符在开头或者结尾,则转成数组后,生成一个空字符串的元素。

split 还有第二个参数:

第二个参数代表转成数组后,所保留的元素的数量。(从以转化好的数组下标为0的元素开始)

有两个参数

第一个参数:起始的位置

第二个参数:截取的数量

不会改变原数组

会将截取到的字符串以返回值的形式返回出来

有两个参数

第一个参数:起始的位置(返回时包括位置所对应的字符)

第二个参数:结束的位置(返回时不包括位置所对应的字符)

不会改变原数组

会将截取到的字符串以返回值的形式返回出来

不会改变原数组

会将转换好的字符串以返回值的形式返回出来

不会改变原数组

会将转换好的字符串以返回值的形式返回出来