一、bold()正則表達式
方法用於把字符串顯示爲粗體。語法: stringObject.bold() 數組
以下,對str進行bold操做以後,實際上時對這個字符串加了<b>標籤,在文檔中將以粗體進行展現函數
let str = 'Hello world' let str1 = str.bold() console.log(str1)//<b>Hello world</b> document.write(str1)//文檔中顯示:Hello world
二、charAt()this
返回指定位置的字符。注意,JavaScript 並無一種有別於字符串類型的字符數據類型,因此返回的字符是長度爲 1 的字符串。語法: stringObject.charAt(index) ,其中index爲必須,表示字符串中某個字符的下標編碼
let str = 'Hello world' let str1 = str.charAt(1) console.log(str1)//e
三、charCodeAt()spa
返回指定位置的字符的 Unicode 編碼。這個返回值是 0 - 65535 之間的整數。方法 charCodeAt() 與 charAt() 方法執行的操做類似,只不過前者返回的是位於指定位置的字符的編碼,然後者返回的是字符子串。語法: stringObject.charCodeAt(index) ,index爲必須,表示字符在字符串中的下標code
let str = 'Hello world' let str1 = str.charCodeAt(1) console.log(str1)//101
四、concat()regexp
用於鏈接兩個或者多個字符串。語法: stringObject.concat(stringX,stringX,...,stringX) ,stringX爲必須,concat方法將全部的參數轉換爲字符串,而後按順序鏈接到字符串stringObject的末尾,並返回鏈接後的字符串。單stringObjec並未被修改。與Array.concat()很類似。可是,一般使用‘+’運算符來進行字符串的拼接會更爲簡便一些,並且使用的居多。對象
let str1 = 'Hello ' let str2 = 'world' let str3 = str1.concat(str2) console.log(str3)//'Hello world'
五、fontColor()blog
用於按照指定的顏色來顯示字符串。語法: stringObject.fontcolor(color) 。參數color必需。爲字符串規定 font-color。該值必須是顏色名(red)、RGB 值(rgb(255,0,0))或者十六進制數(#FF0000)。
實際就是給字符串加一個font標籤,可是如今font已經被棄用了,這個方法也是沒有用處了。
let str="Hello world!" let str1 = str.fontcolor("Red") console.log(str1)//<font color="Red">Hello world!</font> document.write(str1)//Hello world!
六、fontSize()
方法用於按照指定的尺寸來顯示字符串。語法: stringObject.fontsize(size) 。size 參數必須是從 1 至 7 的數字。
七、fromCharCode()
fromCharCode() 可接受一個指定的 Unicode 值,而後返回一個字符串。語法: String.fromCharCode(numX,numX,...,numX) 。必需。一個或多個 Unicode 值,即要建立的字符串中的字符的 Unicode 編碼。它不能做爲您已建立的 String 對象的方法來使用。所以它的語法應該是 String.fromCharCode(),而不是 myStringObject.fromCharCode()。
let str=String.fromCharCode(72,69,76,76,79) console.log(str)//HELLO
八、indexOf()
indexOf() 方法可返回某個指定的字符串值在字符串中首次出現的位置。語法: stringObject.indexOf(searchvalue,fromindex) ,searchvalue爲必需。規定需檢索的字符串值。fromindex可選的整數參數。規定在字符串中開始檢索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略該參數,則將從字符串的首字符開始檢索。indexOf() 方法對大小寫敏感!若是要檢索的字符串值沒有出現,則該方法返回 -1。
let str='Hello world' console.log(str.indexOf('Hello'))//0 console.log(str.indexOf('world'))//6 console.log(str.indexOf('World'))//-1
九、italics()
用於把字符串顯示爲斜體。語法: stringObject.italics()
十、lastIndexOf()
lastIndexOf() 方法可返回一個指定的字符串值最後出現的位置,在一個字符串中的指定位置從後向前搜索。語法: stringObject.lastIndexOf(searchvalue,fromindex) 。searchvalue爲必需。規定需檢索的字符串值。fromindex可選的整數參數。規定在字符串中開始檢索的位置。它的合法取值是 0 到 stringObject.length - 1。如省略該參數,則將從字符串的最後一個字符處開始檢索。若是在 stringObject 中的 fromindex 位置以前存在 searchvalue,則返回的是出現的最後一個 searchvalue 的位置。lastIndexOf() 方法對大小寫敏感!若是要檢索的字符串值沒有出現,則該方法返回 -1。
let str='Hello world' console.log(str.lastIndexOf('Hello'))//0 console.log(str.lastIndexOf('world'))//6 console.log(str.lastIndexOf('World'))//-1
十一、match()
該方法能夠在字符串內檢索指定的值,或找到一個或多個正則表達式的匹配。語法: stringObject.match(searchvalue) stringObject.match(regexp) 。返回值是匹配結果的數組,該數組的內容依賴於regexp是否具備全局標誌g。
match() 方法將檢索字符串 stringObject,以找到一個或多個與 regexp 匹配的文本。這個方法的行爲在很大程度上有賴於 regexp 是否具備標誌 g。
若是 regexp 沒有標誌 g,那麼 match() 方法就只能在 stringObject 中執行一次匹配。若是沒有找到任何匹配的文本, match() 將返回 null。不然,它將返回一個數組,其中存放了與它找到的匹配文本有關的信息。該數組的第 0 個元素存放的是匹配文本,而其他的元素存放的是與正則表達式的子表達式匹配的文本。除了這些常規的數組元素以外,返回的數組還含有兩個對象屬性。index 屬性聲明的是匹配文本的起始字符在 stringObject 中的位置,input 屬性聲明的是對 stringObject 的引用。
若是 regexp 具備標誌 g,則 match() 方法將執行全局檢索,找到 stringObject 中的全部匹配子字符串。若沒有找到任何匹配的子串,則返回 null。若是找到了一個或多個匹配子串,則返回一個數組。不過全局匹配返回的數組的內容與前者大不相同,它的數組元素中存放的是 stringObject 中全部的匹配子串,並且也沒有 index 屬性或 input 屬性。
let str='Hello world ! this is a peaceful world' console.log(str.match('Hello'))//["Hello", index: 0, input: "Hello world ! this is a peaceful world", groups: undefined] console.log(str.match('world'))//["world", index: 6, input: "Hello world ! this is a peaceful world", groups: undefined] console.log(str.match('World'))//null
let str1 = '1 one 2 two 3 three 4 four'
console.log(str1.match(/\d+/g))//["1", "2", "3", "4"]
十二、replace()
該方法用於在字符串中用一些字符替換其餘字符,或者替換一個與正則表達式匹配的字符。語法: stringObject.replace(regexp/substr,replacement) 。字符串 stringObject 的 replace() 方法執行的是查找並替換的操做。它將在 stringObject 中查找與 regexp 相匹配的子字符串,而後用 replacement 來替換這些子串。若是 regexp 具備全局標誌 g,那麼 replace() 方法將替換全部匹配的子串。不然,它只替換第一個匹配子串。replacement 能夠是字符串,也能夠是函數。若是它是字符串,那麼每一個匹配都將由字符串替換。可是 replacement 中的 $ 字符具備特定的含義。以下表所示,它說明從模式匹配獲得的字符串將用於替換。
let str='Hello world ! this is a peaceful world' console.log(str.replace(/Hello/,'nice'))//nice world ! this is a peaceful world console.log(str.replace(/world/g,'cnblogs'))//Hello cnblogs ! this is a peaceful cnblogs
1三、search()
該方法用於檢索字符串中指定的子字符串,或檢索與正則表達式相匹配的子字符串。語法: stringObject.search(regexp) 。regexp參數能夠是須要在 stringObject 中檢索的子串,也能夠是須要檢索的 RegExp 對象.要執行忽略大小寫的檢索,請追加標誌 i。stringObject 中第一個與 regexp 相匹配的子串的起始位置。若是沒有找到任何匹配的子串,則返回 -1。search() 方法不執行全局匹配,它將忽略標誌 g。它同時忽略 regexp 的 lastIndex 屬性,而且老是從字符串的開始進行檢索,這意味着它老是返回 stringObject 的第一個匹配的位置。
let str='Hello world ! this is a peaceful world' console.log(str.search(/Hello/))//0 console.log(str.search(/world/))//6
1四、slice()
該方法可提取字符串的某個部分,並以新的字符串返回被提取的部分。一個新的字符串。包括字符串 stringObject 從 start 開始(包括 start)到 end 結束(不包括 end)爲止的全部字符。語法: stringObject.slice(start,end) 。String 對象的方法 slice()、substring() 和 substr() (不建議使用)均可返回字符串的指定部分。slice() 比 substring() 要靈活一些,由於它容許使用負數做爲參數。slice() 與 substr() 有所不一樣,由於它用兩個字符的位置來指定子串,而 substr() 則用字符位置和長度來指定子串。
let str='Hello world ! this is a peaceful world' console.log(str.slice(6))//world ! this is a peaceful world console.log(str.slice(6,14))//world !
1五、split()
該方法用於把一個字符串分割成字符串數組。語法: stringObject.split(separator,howmany) 。separator必需。字符串或正則表達式,從該參數指定的地方分割 stringObject。howmany可選。該參數可指定返回的數組的最大長度。若是設置了該參數,返回的子串不會多於這個參數指定的數組。若是沒有設置該參數,整個字符串都會被分割,不考慮它的長度。返回一個字符串數組。該數組是經過在 separator 指定的邊界處將字符串 stringObject 分割成子串建立的。返回的數組中的字串不包括 separator 自身。可是,若是 separator 是包含子表達式的正則表達式,那麼返回的數組中包括與這些子表達式匹配的字串(但不包括與整個正則表達式匹配的文本)。若是把空字符串 ("") 用做 separator,那麼 stringObject 中的每一個字符之間都會被分割。String.split() 執行的操做與 Array.join 執行的操做是相反的。
let str='Hello world ! this is a peaceful world' console.log(str.split(" "))//["Hello", "world", "!", "this", "is", "a", "peaceful", "world"] console.log(str.split(""))//["H", "e", "l", "l", "o", " ", "w", "o", "r", "l", "d", " ", "!", " ", "t",
"h", "i", "s", " ", "i", "s", " ", "a", " ", "p", "e", "a", "c", "e", "f", "u", "l", " ", "w", "o", "r", "l", "d"] console.log(str.split(" ",3))//["Hello", "world", "!"]
1六、strike()
1七、sub()
1八、substr()
該方法可在字符串中抽取從 start 下標開始的指定數目的字符。ECMAscript 沒有對該方法進行標準化,所以反對使用它。
1九、substring()
該方法用於提取字符串中介於兩個指定下標之間的字符。語法: stringObject.substring(start,stop) 。返回一個新的字符串,該字符串值包含 stringObject 的一個子字符串,其內容是從 start 處到 stop-1 處的全部字符,其長度爲 stop減 start。substring() 方法返回的子串包括 start 處的字符,但不包括 stop 處的字符。若是參數 start 與 stop 相等,那麼該方法返回的就是一個空串(即長度爲 0 的字符串)。若是 start 比 stop 大,那麼該方法在提取子串以前會先交換這兩個參數。與slice()方法不一樣的是,substring() 不接受負的參數。
let str='Hello world ! this is a peaceful world' console.log(str.substring(6))//world ! this is a peaceful world
20、sup()
該方法用於把字符串轉換爲小寫。語法: stringObject.toLocaleLowerCase() 。返回一個新的字符串,在其中 stringObject 的全部大寫字符所有被轉換爲了小寫字符。與 toLowerCase() 不一樣的是,toLocaleLowerCase() 方法按照本地方式把字符串轉換爲小寫。只有幾種語言(如土耳其語)具備地方特有的大小寫映射,全部該方法的返回值一般與 toLowerCase() 同樣。
let str='Hello World ! This is a peaceful world' console.log(str.toLocaleLowerCase())//hello world ! this is a peaceful world
2二、toLocaleUpperCase()
該方法用於把字符串轉換爲大寫。語法: stringObject.toLocaleUpperCase() 。返回一個新的字符串,在其中 stringObject 的全部小寫字符所有被轉換爲了大寫字符。與 toUpperCase() 不一樣的是,toLocaleUpperCase() 方法按照本地方式把字符串轉換爲大寫。只有幾種語言(如土耳其語)具備地方特有的大小寫映射,全部該方法的返回值一般與 toUpperCase() 同樣。
let str='Hello World ! This is a peaceful world' console.log(str.toLocaleLowerCase())//HELLO WORLD ! THIS IS A PEACEFUL WORLD
2三、toLowerCase()
該方法用於把字符串轉換爲小寫。語法: stringObject.toLowerCase()返回一個新的字符串,在其中 stringObject 的全部大寫字符所有被轉換爲了小寫字符
let str='Hello World ! This is a peaceful world' console.log(str.toLowerCase())//hello world ! this is a peaceful world
2四、toUpperCase()
該方法用於把字符串轉換爲大寫。語法: stringObject.toUpperCase() 。返回一個新的字符串,在其中 stringObject 的全部小寫字符所有被轉換爲了大寫字符。
let str='Hello World ! This is a peaceful world' console.log(str.toUpperCase())//HELLO WORLD ! THIS IS A PEACEFUL WORLD
2五、trim()
該方法用來刪除字符串先後的空格。用法: stringObject.trim()
let str=' Hello World ! This is a peaceful world ' console.log('('+str.trim()+')')//(Hello World ! This is a peaceful world)