String類型是字符串的對象包裝類型,可以使用String構造函數來創建
var stringObject = new String("hello world");charAt():以單字符字符串的形式返回給定位置的字符 charCodeAt(): 返回該位置字符的字符編碼 是用于訪問字符串中特定字符的方法,都接收一個參數,即基于0的字符位置。
contact():用于將一個或者多個字符串拼接起來,返回拼接得到的新字符串 contact()方法可以接受任意多個參數,也就是說可以通過它拼接任意多個字符串。 ECMAScript還提供了三個基于字符串創建新字符串的方法:slice()、substr()、substring() 這三個方法都會返回被操作字符串的一個子字符串,而且也都接受一或兩個參數。 第一個參數指定字符串的開始位置,第二個參數(在特定情況下)表示字符串到哪里結束。 具體來說,slice()和substring()的第二個參數指定的是子字符串最后一個字符后面的位置,而substr()的第二個參數指定的則是返回的字符個數。如果沒有給這些方法傳遞第二個參數,則將字符串的長度作為結束位置。同樣,這三個方法不會修改字符串本身的值。
var stringValue = "Hello world";console.log(stringValue.slice(3));//"lo world"console.log(stringValue.substring(3));//"lo world"console.log(stringValue.substr(3));//"lo world"console.log(stringValue.slice(3,7));//"lo w"console.log(stringValue.substring(3,7));//"lo w"console.log(stringValue.substr(3,7));//"lo worl"http://substr(3,7)從3開始返回7個字符,空格也占位在給slice()和substr()傳遞一個負值參數時,他們的行為相同(加上字符串的長度),但substring()會返回全部字符串因為會將負數轉化為0.
var stringValue = "hello world";console.log(stringValue.slice(-3));//"rld"console.log(stringValue.substring(-3));//"hello world"console.log(stringValue.substr(-3));//"rld"console.log(stringValue.slice(3,-4));//"lo w"(3,7)console.log(stringValue.substring(3,-4));//"hel"(3,0)console.log(stringValue.substr(3,-4));//""(空字符串)substring()參數為負數時,會自動轉化為0.
從字符串中查找子字符串的方法:indexOf()和lastIndexOf().從一個字符串中搜索給定的子字符串,然后返回子字符串的位置(如果沒有找到該子字符串,則返回-1)indexOf()從前向后搜索,lastIndexOf()從后向前搜索。 接受可選的第二個參數,表示開始搜索的位置。
ECMAScript為所有字符串定義了trim()方法。這個方法會創建一個字符串的副本,刪除前置及后綴的所有空格,然后返回結果。
var stringValue = " hello world ";var trimmedStringValue = stringValue.trim();console.log(stringValue);//" hello world "console.log(trimmedStringValue);//"hello world"只刪除前后綴空格
toLowerCase() toLocaleLowerCase() toUpperCase() toLocaleUpperCase()
toLocaleLowerCase()和toLocaleUpperCase()是針對特定地區的實現
String類型定義了幾個用于在字符串中匹配模式的方法。 match()本質上與RegExp的exec()方法相同。match()方法只接受一個參數,正則表達式或者是一個RegExp對象
var text = "cat,bat,sat,fat";var pattern = /.at/;//與pattern.exec(text)相同var matches = text.match(pattern);console.log(matches.index);//0console.log(matches[0]);//"cat"console.log(pattern.lastIndex);//0Array[1]0:"cat"index:0input:"cat,bat,sat,fat"length:1本例中match()方法返回了一個數組,數組的第一項是與整個模式匹配的字符串,之后的每一項(如果有)保存著與正則表達式中的捕獲組匹配的字符串。 match 方法返回的數組有三個屬性:input、index和lastIndex Input 屬性包含整個的被查找字符串。Index 屬性包含了在整個被查找字符串中匹配的子字符串的位置。LastIndex 屬性包含了最后一次匹配中最后一個字符的下一個位置。 另一個用于查找模式的方法是search(),這個方法的唯一參數與match()方法的參數相同:由字符串或RegExp對象指定的一個正則表達式。 search()方法返回字符串中第一個匹配項的索引:如果沒有找到匹配項,則返回-1.而且,search()方法始終是從字符串開頭向后查找模式。
var text = "cat,bat,sat,fat";var pos = text.search(/at/);//參數:正則表達式console.log(pos);//1replace()方法,接受兩個參數:第一個參數可以是一個RegExp對象或者一個字符串,第二個參數可以是一個字符串或者一個函數。 如果第一個參數是字符串,那么只會替換第一個子字符串。要想替換所有字符串,唯一的辦法就是提供一個正則表達式,需要指定全局(g)標志。
var text = "cat,bat,sat,fat";var result = text.replace("at","ond");console.log(result);//"cond,bat,sat,fat"result = text.replace(/at/g,"ond");console.log(result);//"cond,bond,sond,fond"split()字符串分隔函數 可以基于指定的分隔符將一個字符串分隔成多個字符串,并將結果放在一個數組中。 該方法接受可選的第 二個參數,用于指定數組的大小,以確保返回的數組不會超過既定大小。
localeCompare():用于比較兩個字符串
var stringValue = "yellow";console.log(stringValue.localeCompare("brick"));//1,y<bconsole.log(stringValue.localeCompare("yellow"));//0,y=yconsole.log(stringValue.localeCompare("zoo"));//-1,y>zfromCharCode()方法接收一或多個字符編碼,然后將他們轉換成一個字符串。
新聞熱點
疑難解答