字符串就是零個(gè)或多個(gè)排在一起的字符趴酣,放在單引號(hào)或雙引號(hào)之中威兜。
"abc"
'abc'
單引號(hào)字符串的內(nèi)部,可以使用雙引號(hào)喂窟。雙引號(hào)字符串的內(nèi)部测暗,可以使用單引號(hào)。
"It's a long journey"
'key = "value"'
如果長(zhǎng)字符串必須分成多行磨澡,可以在每一行的尾部使用反斜杠碗啄。
var longString = "Long \
long \
long \
string";
加了反斜杠以后,原來寫在一行的字符串稳摄,可以分成多行書寫稚字。但是輸出的時(shí)候還是單行,效果與寫在同一行完全一樣。注意胆描,反斜杠的后面必須是換行符瘫想,而不能有其他字符(比如空格),否則會(huì)報(bào)錯(cuò)昌讲。
轉(zhuǎn)義:
如果要在單引號(hào)字符串的內(nèi)部国夜,使用單引號(hào)(或者在雙引號(hào)字符串的內(nèi)部,使用雙引號(hào))短绸,就必須在內(nèi)部的單引號(hào)(或者雙引號(hào))前面加上反斜杠车吹,用來轉(zhuǎn)義。
'Did she say \'Hello\'?'
// "Did she say 'Hello'?"
需要用反斜杠轉(zhuǎn)義的特殊字符醋闭,主要有下面這些:
\0 null(\u0000)
\b 后退鍵(\u0008)
\f 換頁符(\u000C)
\n 換行符(\u000A)
\r 回車鍵(\u000D)
\t 制表符(\u0009)
\v 垂直制表符(\u000B)
\' 單引號(hào)(\u0027)
\" 雙引號(hào)(\u0022)
\ 反斜杠(\u005C)
任何字符串的長(zhǎng)度都可以通過訪問其 length 屬性獲得窄驹,例如:
var myStr = 'the quick brown fox jumps over the lazy dog';
console.log(myStr.length) //43
轉(zhuǎn)換為字符串:
toString方法:
數(shù)值、布爾值证逻、對(duì)象和字符串值都有toString()方法乐埠。但null和undefined值沒有這種方法。故在不知道要轉(zhuǎn)型的值是不是null或undefined的情況下瑟曲,可用轉(zhuǎn)型函數(shù)String()饮戳,這個(gè)函數(shù)可以將任何類型的值轉(zhuǎn)換為字符串。
alert(10.toString()); //"10"
alert(10.toString(2)); //"1010" //2進(jìn)制
alert(10.toString(8)); //"12" //8進(jìn)制
String(null); //"null"
var a;
String(a); // "undefined"
String(true) //"true"
String對(duì)象:
三種基本構(gòu)造函數(shù)之一洞拨,用String構(gòu)造函數(shù)來創(chuàng)建扯罐。
var stringObject = new String("hello world")
typeof stringObject //"object"
1.charAt()用于訪問字符串中特定字符的方法:
var stringValue = "hello world"
alert(stringValue.charAt(1)); //"e"
此方法完全可以用數(shù)組下標(biāo)替代。
'abc'[1] // "b"
2.String.fromCharCode()是一個(gè)靜態(tài)方法烦衣。該方法的參數(shù)是一系列Unicode碼點(diǎn)歹河,返回對(duì)應(yīng)的字符串。與之相對(duì)的是charCodeAt()方法花吟,返回給定位置字符的Unicode碼點(diǎn)秸歧。
String.fromCharCode(104, 101, 108, 108, 111); // "hello"
'abc'.charCodeAt(1) // 98
3.concat()方法用于將一個(gè)或多個(gè)字符串拼接起來,返回一個(gè)新字符串衅澈。
var myString = "hello";
console.log(myString.concat(" world")) //"hello world"
console.log(myString.concat(" world","!")); //"hello world!"
4.slice(),substr(),substring()三個(gè)方法是基于子字符串創(chuàng)建新字符串的方法键菱。slice()和substr()的第一個(gè)參數(shù)是子字符串的開始位置,第二個(gè)參數(shù)是子字符串的結(jié)束位置(不含該位置)今布。而substring()的第二個(gè)參數(shù)是反回的字符個(gè)數(shù)经备。
var myString = "hello world";
console.log(myString.slice(3)); //"lo world"
console.log(myString.substring(3)); //"lo world"
console.log(myString.sustr(3)); //"lo world"
console.log(myString.slice(3,7)); //"lo w"
console.log(myString.substring(3,7)); //"lo w"
console.log(myString.substr(3,7)); //"lo worl"
如果傳遞參數(shù)為負(fù)值時(shí),三個(gè)方法的行為則不盡相同部默。slice()方法將兩個(gè)負(fù)參數(shù)都與字符串的長(zhǎng)度相加侵蒙;substring()將兩個(gè)負(fù)參數(shù)都轉(zhuǎn)換為0;而substr()將第一個(gè)負(fù)參數(shù)與字符串長(zhǎng)度相加傅蹂,將第二個(gè)負(fù)參數(shù)轉(zhuǎn)換為0纷闺。
var myString = "hello world";
console.log(myString.slice(-3)); //"rld"
console.log(myString.substring(-3)); //"rld"
console.log(myString.substr(-3)); //"hello world"
console.log(myString.slice(3,-4)); //"lo w"
console.log(myString.substring(3,-4)); //"hel"
console.log(myString.substr(3,-4)); //""空字符串
5.indexOf()與lastIndexOf()
這兩個(gè)方法用于確定一個(gè)字符串在另一個(gè)字符串中的位置算凿,都返回一個(gè)整數(shù),表示匹配開始的位置犁功。如果返回-1氓轰,就表示不匹配。兩者的區(qū)別在于浸卦,indexOf從字符串頭部開始匹配戒努,lastIndexOf從尾部開始匹配。它們還可以接受第二個(gè)參數(shù)镐躲,對(duì)于indexOf方法,第二個(gè)參數(shù)表示從該位置開始向后匹配侍筛;對(duì)于lastIndexOf萤皂,第二個(gè)參數(shù)表示從該位置起向前匹配。
var myString = "hello world";
console.log(myString.indexOf("o")); //"4"
console.log(myString.lastIndexOf("o")); //"7"
console.log(myString.indexOf("o",6)); //"7"
console.log(myString.lastIndexOf("o",6)); //"4"
6.trim()方法用于去除字符串兩端的空格匣椰,返回一個(gè)新字符串裆熙,該方法去除的不僅是空格,還包括制表符(\t禽笑、\v)入录、換行符(\n)和回車符(\r)。
' hello world '.trim() // "hello world"
'\r\nabc \t'.trim() // 'abc'
7.toLowerCase()方法用于將一個(gè)字符串全部轉(zhuǎn)為小寫佳镜,toUpperCase()則是全部轉(zhuǎn)為大寫僚稿。它們都返回一個(gè)新字符串,不改變?cè)址吧臁_@個(gè)方法也可以將布爾值或數(shù)組轉(zhuǎn)為大寫字符串蚀同,但是需要通過call方法使用。
var myString = "Hello World";
console.log(myString.toLowerCase()); //"hello world"
console.log(myString.toUpperCase()); //"HELLO WORLD"
console.log(myString.toUpperCase.call(true)); //"TRUE"
console.log(myString.toLowerCase.call(['A','B','c'])); //"a,b,c"
8.split()方法按照指定規(guī)則分割字符串啊掏,返回一個(gè)由分割出來的子字符串組成的數(shù)組蠢络。
split方法還可以接受第二個(gè)參數(shù),限定返回?cái)?shù)組的最大成員數(shù)迟蜜。
'a|b|c'.split('|') // ["a", "b", "c"]
'a|b|c'.split('|', 2) // ["a", "b"]