字符串的方法
charAt() 輸入下標(biāo)返回指定字符
var stringValue = "hello world";
alert(stringValue.charAt(1)); //"e"
charCodeAt() 輸入下標(biāo)返回unicode編碼 返回值為 0 - 65535 之間的整數(shù)
var stringValue = "hello world";
alert(stringValue.charCodeAt(1)); //輸出"101"
[i] 加方括號訪問指定字符
var stringValue = "hello world";
alert(stringValue[1]); //"e"
concat() 將一或多個字符串拼接起來供鸠,返回拼接得到的新字符串 (可以使用加號操作符代替更快捷)
var stringValue = "hello ";
var result = stringValue.concat("world", "!");//連接兩個字符
alert(result);//"hello world!"
slice()玻孟、substr()和 substring()
正值時第一個參數(shù)指定子字符串的開始位置
slice()和 substring()的第二個參數(shù)指定的是子字符串最后一個字符后面的位置 不包括自身漏益。
substr()的第二個參數(shù)指定的則是返回的字符個數(shù) 不包括自身穆桂。沒有給這些方法傳遞第二個參數(shù),則將字符串的長度作為結(jié)束位置瑟幕。
var stringValue = "hello world";
alert(stringValue.slice(3));//"lo world"
alert(stringValue.substring(3));//"lo world"
alert(stringValue.substr(3));//"lo world"
alert(stringValue.slice(3, 7));//"lo w"
alert(stringValue.substring(3,7));//"lo w"
alert(stringValue.substr(3, 7));//"lo worl" 下標(biāo)3開始截取7個
負(fù)值
var stringValue = "hello world";
alert(stringValue.slice(-3));//"rld" //從右往左取 3個下標(biāo)1開始
alert(stringValue.substring(-3));//"hello world"
alert(stringValue.substr(-3)); //"rld"/從右往左取 3個下標(biāo)1開始
alert(stringValue.slice(3, -4));//"lo w"
alert(stringValue.substring(3, -4));//"hel"
alert(stringValue.substr(3, -4));//""(空字符串)
indexOf() 輸入查找字符串最先出現(xiàn)位置的下標(biāo)(從左開始搜索)
lastindexOf() 輸入查找字符串最先出現(xiàn)位置的下標(biāo)(從右開始搜索)
如果沒有找到該子字符串磕蒲,則返回-1
如果一個字符串中僅出現(xiàn)了一次,那么 indexOf()和 lastIndexOf()會返回相同的位置值
var stringValue = "hello world";
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7
var stringValue = "hello world";
alert(stringValue.indexOf("o", 6)); //7 從下標(biāo)6開始搜索往右第一個出現(xiàn)的o
alert(stringValue.lastIndexOf("o", 6)); //4 從下標(biāo)6開始搜索往左第一個出現(xiàn)的o
trim() 創(chuàng)建一個字符串的副本只盹,刪除字符串開頭結(jié)尾的所有空格辣往,然后返回結(jié)果。
var stringValue = " hello world ";
var trimmedStringValue = stringValue.trim();
alert(stringValue); //" hello world "
alert(trimmedStringValue); //"hello world"
toLowerCase()和 toUpperCase() 大小寫轉(zhuǎn)換
oLocaleLowerCase()和 toLocaleUpperCase() 大小寫轉(zhuǎn)換 特定地區(qū)的實現(xiàn)大小寫轉(zhuǎn)換
var stringValue = "hello world";
alert(stringValue.toLocaleUpperCase()); //"HELLO WORLD"
alert(stringValue.toUpperCase()); //"HELLO WORLD"
alert(stringValue.toLocaleLowerCase()); //"hello world"
alert(stringValue.toLowerCase()); //"hello world"
字符串的模式匹配方法
match() 輸入只接受一個參數(shù)殖卑,要么是一個正則表達(dá)式站削,要么是一個 RegExp 對象。
var text = 'cat,bat,dat,lat';
var pattern = /.at/;
//等價于pattern.exec(text) == text.match(pattern);
var matches = text.match(pattern);
alert(matches.index); //0
alert(matches[0]); //"cat"
alert(pattern.lastIndex); //0
search() 返回字符串中第一個匹配項的索引;如果沒有找到匹配項孵稽,則返回-1许起。 而且十偶,search()方法始終是從字符串開頭向后查找模式。只接受一個參數(shù)园细,要么是一個正則表達(dá)式惦积,要么是一個 RegExp 對象。
var text = "cat, bat, sat, fat";
var pos = text.search(/at/);
alert(pos); //1
replace()替換數(shù)組
第一個參數(shù)可以是一個 RegExp 對象或者一個字符串(這個字符串不會被轉(zhuǎn)換成正則表達(dá)式)
第二個參數(shù)可以是一個字符串或者一個函數(shù)猛频。如果第一個參數(shù)是字符串狮崩,那么只會替換第一個子字符串。
想替換所有子字符串鹿寻,唯一的辦法就是提供一個正則表達(dá)式睦柴,而且要指定全局(g)標(biāo)志
1是匹配第一個捕獲組的子字符串(一般就是字符串本身),
nn 匹配第nn個捕獲組的子字符串财搁,其中nn等于01~99训柴。例如,
02 是匹配第二個捕獲組的子字符串,以此類推洗鸵。如果正則表達(dá)式中沒有定義捕獲組越锈,則使用空字符串
var text = "cat, bat, sat, fat";
var result = text.replace("at", "ond");
alert(result); //"cond, bat, sat, fat"
result = text.replace(/at/g, "ond");
alert(result); //"cond, bond, sond, fond"
如果第二個參數(shù)是字符串 可以使用特殊字符 nn
var text = "cat, bat, sat, fat";
result = text.replace(/(.at)/g, "word ($1)");//$1是表示第一個捕獲組的子字符串
alert(result); //word (cat), word (bat), word (sat), word (fat)
split() 基于指定的分隔符將一個字符串分割成多個子字符串,分隔符可以是字符串,也可以是一個 RegExp 對象(這個方 法不會將字符串看成正則表達(dá)式)膘滨。split()方法可以接受可選的第二個參數(shù)甘凭,用于指定數(shù)組的大小, 以便確保返回的數(shù)組不會超過既定大小火邓。
var colorText = "red,blue,green,yellow";
var colors1 = colorText.split(",");//["red", "blue", "green", "yellow"]
var colors2 = colorText.split(",", 2);//["red", "blue"]
var colors3 = colorText.split(/[^\,]+/);//["", ",", ",", ",", ""]
localeCompare() 比較兩個字符串大小丹弱,并返回值中的一個
var stringValue = "yellow";
alert(stringValue.localeCompare("brick")); //1 因為"brick"在 字母表中排在"yellow"之前,所以 localeCompare()返回了 1
alert(stringValue.localeCompare("yellow")); //0 字符串相等返回0
lert(stringValue.localeCompare("zoo")); //-1 因為"zoo"在字母表中排在"yellow"后面铲咨,所以 localeCompare() 返回了-1
String 構(gòu)造函數(shù)本身還有一個靜態(tài)方法fromCharCode()
fromCharCode()接收一或多個字符編碼躲胳,然后將它們轉(zhuǎn)換成一個字符串。//與 charCodeAt()用法相反
alert(String.fromCharCode(104, 101, 108, 108, 111)); //"hello"