javascript学习笔记
String方法
- 字符方法
- charAt():取出特定位置的字符
sample:var stringValue = "Hello world"; alert(stringValue.charAt(1)) //"e"
- charCodeAt():取出特定位置字符的Unicode编码
sample:var stringValue = "foobar"; alert(stringValue.charCodeAt(3));//"o"
- 字符串操作
-
concat():多个字符串拼接,它会把参数中的数组一项项的添加到字符串中拼接
sample:var stringValue="Hello "; var result=stringValue.concat('wonderful ','world','!')\\"Hello wonderful world!" result=stringValue.concat(['you','jjj'],"1");\\"Hello you,jjj1"
-
slice():返回子字符串,如果没有第二个参数,那么返回从第一个参数到最后的所有内容
如果有第二个参数那么返回从第一个参数起始到第二个参数截止的所有内容,不包含第二个
参数的那个字符,如果参数中有负数,表示倒数第几,
sample:var stringValue="Hello world"; alert(stringValue.slice(3));//从第三开始到左后'lo world' alert(stringValue.slice(2,8));//从第二开始到第8(不含第八)"llo wo" alert(stringValue.slice(-3));//从倒数第三开始到最后 "rld" alert(stringValue.slice(-4,-1));//从倒数第四开始到倒数第一(不包含) "orl"
-
substr():如果参数中没有负数,那么和slice完全一样,如果参数中有负数。就有
差别了。sybstr把第一个参数作为倒数第几,第二个参数是负数的话,自动转换为0
sample:var stringValue="Hello world"; alert(stringValue.substr(-3));//从倒数第三开始到最后 "rld" alert(stringValue.substr(-3,-2));//从倒数第三开始第一个 就是空 ""
-
substring:第一个参数是起始位置,第二个参数是返回的字符个数。如果没有
第二个参数,那么返回从起始到最后的所有字符。如果有负数,所有负数的参数
都会设置为0
sample:var stringValue="Hello world"; alert(stringValue.substring(-3));//""
-
字符串位置方法:
stringA.indexOf(stringB): 从左往右的在A字符串中寻找B字符串第一次出现的位置,
如果没找到,返回-1。 第二个参数是从哪个字符开始查找。
stringA.lastIndexOf(stringB):从右往左的在A字符串中寻找B字符串第一次出现的位置,
如果没找到,返回-1。 第二个参数是从哪个字符开始查找。
sample1:var strA="Hello,world!"; alert(strA.lastIndexOf("o"));//7 alert(strA.indexOf('o'));//4 alert(strA.indexOf("o",6));//7 alert(strA.lastIndexOf("o",6));//4
sample2:
var strA="Geer is a teacher and her father is layer!"; var position=new Array(); var pos = strA.indexOf('er'); while (pos > -1){ position.push(pos); pos = strA.indexOf('er',pos+2); } alert(position); //[2, 15, 23, 30, 39]
-
trim():返回去掉字符串两边的空格之后的字符串。
sample:var strA = " Hello world! " alert(strA.trim());//"Hello world!"
字符串大小写转换:
toLowerCase()
toLocaleLowerCase()//基本不用
toUpperCase()
toLocaleUpperCase()//基本不用-
字符串的模式匹配方法:
match()
sample:var text='cat,bat,sat,fat'; var pattern = /.at/; //与pattern.exec(text)相同 var matchs=text.match(pattern);//返回一个数组
search():与indexOf类似,返回从左往右寻找匹配模式的
第一次出现的位置
sample:var text = "cat, bat, sat, fat"; var pos = text.search(/at/); alert(pos); //1
replace():第一个参数是RegExp或者字符串,第二个参数是
一个字符串或者一个函数,如果是字符串就是替换的字符串。如果
想替换所有字字符串,那需要使用正则表达式。
replace()方法的第二个参数也可以是一个函数,需要向这个函数
传递三个参数,模式的匹配项、匹配项在字符串中的位置、原始字符串
_sample_1:var text = "cat,bat,sat,fat"; var result = text.replace("at","ond");//只替换第一个at result=text.replace(/at/g,"ond");//字符串中所有的at都替换为ond result=text.replace(/(.at)/g,"find($1)");//"find(cat),find(bat),find(sat),find(fat)"
sample2:
function htmlEscape(text){ return text.replace(/[<>"&]/g,function(match,pos,originalText){ switch(match){ case "<": return "<"; case ">": return ">"; case "&": return "&"; case "\"": return """; } }); } alert(htmlEscape("<p class=\"greeting"))
split():把字符串按照指定分隔符分割,放在数组中。
sample:var color = "red,blue,yellow,brwon"; var arrC1 = color.split(",");//["red", "blue", "yellow", "brown"] var arrC2 = color.split(",",2)//指定返回数组大小["red","blue"]
-
strA.localCompare(strB)方法:
如果strA排在strB的前面返回负数,-1
如果strA==strB,返回0;
如果strA排在strB的后面返回正数,1
sample:var strA="Hello"; alert(strA.localeCompare("zoo"));//-1 alert(strA.localeCompare("aoo"));//1
fromCharCode():静态方法,从unicode返回字符