【小白笔记】js 字符串操作

JavaScript015

【小白笔记】js 字符串操作,第1张

var str = "hello world"

console.log(str.length)// 输出结果:11

var str = "apple"

var str1 = str.indexOf("p")

var str2 = str.indexOf("h")

var str3 = str.indexOf("pl")

var str4 = str.indexOf("pe")

console.log(str1)// 输出结果:1

console.log(str2)// 输出结果:-1

console.log(str3) // 输出结果:2

console.log(str4) // 输出结果:-1

注:多用来校测某一字符串中是否含有某一子串

var str = "helloworld"

var str1 = str.replace('world','apple')

console.log(str1)// 输出结果:helloapple

注:多与正则配合使用

eg.字符串去所有空格

var str = " hello world ! "

var str1 = str.replace(/\s/g, "")

console.log(str1)// 输出结果:helloworld!

eg.jQuery字符串去首尾两端所有空格方法

var str ="        hello world       "

console.log(str.trim())// 输出结果:hello world

1、substring("起始位置","结束位置[不写时,从起始位置截取到最后]");

2、substr("起始位置","截取长度 [不写时,从起始位置截取到最后]");

var str = "helloworld"

var str1 = str.substring(3,5)

var str2 = str.substring(3)

var str3 = str.substr(3,3)

var str4 = str.substr(3)

console.log(str1)// 输出结果:lo

console.log(str2)// 输出结果:loworld

console.log(str3)// 输出结果:low

console.log(str4)// 输出结果:loworld

注:substring截取,不包含结束位置

slice("起始位置","结束位置 [不写时,从起始位置截取到最后]");

var str = "helloworld"

var str1 = str.slice(1,3)

var str2 = str.slice(5)

console.log(str1)// 输出结果:el

console.log(str2)// 输出结果:world

注: 1、与substring截取类似,不包含结束位置;

        2、 与substring截取不同,slice()结束位置为负时,代表反向位置(如:-1,代表字符串的倒数第一位);

var str = "helloWORLD"

var str1 = str.toLowerCase()

var str2 = str.toUpperCase()

console.log(str1)// 输出结果:helloworld

console.log(str2)// 输出结果:HELLOWORLD

var str = "hello"

var res1 = str.concat(" world ")

var res2 = str.concat(" world ","!")

console.log(res1)// 输出结果:hello world

console.log(res2)// 输出结果:hello world !

注:实际更常用简单的+(加号)

var str = "helloworld"

var str1 = str.charAt(5)

console.log(str1)// 输出结果:w

split("字符串或正则","分割长度[不写时,匹配后每个字符串都被分割]");

var str = "hello world hahaha"

var str1 = str.split(" ")

var str2 = str.split(" ",2)

var str2 = str.split(" ",3)

console.log(str1)// 输出结果:["hello", "world", "hahaha"]

console.log(str2)// 输出结果:["hello", "world"]

console.log(str3)// 输出结果:["hello", "world", "hahaha"]

var str = "hello world"

var str1 = str.match("hello")

var str2 = str.match("helo")

console.log(str1)// 输出结果:hello

console.log(str2)// 输出结果:null

注:与indexOF()、lastIndexOf()的不同之处,match()返回值为字符串,并常配合正则使用

var str = "hello world"

var str1 = str.search("hello")

var str2 = str.search("helo")

console.log(str1)// 输出结果:0

console.log(str2)// 输出结果:-1

注:与match()的不同之处,返回值为字符串中第一次出现所包含 子串或 第一个匹配正则的子串的起始位置

一、函数

 

function trim(str){ //删除左右两端的空格

return str.replace(/(^\s )|(\s $)/g, "")

}

function ltrim(str){ //删除左边的空格

return str.replace(/(^\s*)/g,"")

}

function rtrim(str){ //删除右边的空格

return str.replace(/(\s*$)/g,"")

}

函数调用 trim(str)

二、js对象的方法

String.prototype.trim=function(){

return this.replace(/(^\s )|(\s $)/g, "")

}

String.prototype.ltrim=function(){

return this.replace(/(^\s*)/g,"")

}

String.prototype.rtrim=function(){

return this.replace(/(\s*$)/g,"")

}

类中方法调用 str.trim()

三、将公共方法提取到一个或多个公共js文件,需要的时候直接引用;