typescript筆記(八)

一、string

1)、語法:

var txt = new String("string");

或者更簡單方式:

var txt = "string";

2)、對象屬性:constructor、length、prototype

1、constructor創(chuàng)建該對象的函數(shù)的引用。

例如:

var str = new String ("this is string")

console.log("str.constructor is:"+ str.constructor)? ? // str.constructor is:function String() { [native code] }

2、length返回字符串的長度。

例如:

// var nume = new String('hello')

// console.log("length"+nume.length)? ? ? //5

3、prototype允許您向?qū)ο筇砑訉傩院头椒ā?/p>

例如:

function emp(id:number,name:string){

? ? this.id=id;

? this.name= name;

}

var em = new emp(456,"hebei")

emp.prototype.height = '180'

console.log("員工號"+em.id)? ? //員工號456

console.log("員工姓名"+em.name)? ? //員工姓名hebei

console.log("員工身高"+em.height)? ? ? //員工身高180

3)、對象方法:charAt()、charCodeAt()、concat()、indexOf()、lastIndexOf()、localeCompare()、match()、replace()、search()、slice()、split()、substr()、substring()、toLocaleLowerCase()、toLocaleUpperCase()、toLowerCase()、toString()、toUpperCase()、valueOf()

1、charAt()返回在指定位置的字符。

例如:

var str = new String("round")

console.log("str.charAt(0)" + str.charAt(0))? ? ? ? //str.charAt(0)r

console.log("str.charAt(3)" + str.charAt(3))? ? ? ? //str.charAt(3)n

2、charCodeAt()返回在指定的位置的字符的 Unicode 編碼。

例如:

var str = new String('round')

console.log("str.charCodeAt(0)" + str.charCodeAt(0))

console.log("str.charCodeAt(3)" + str.charCodeAt(3))

console.log("str.charCodeAt(4)" + str.charCodeAt(4))

結(jié)果:

str.charCodeAt(0)114

str.charCodeAt(3)110

str.charCodeAt(4)100

3、concat連接兩個或更多字符串,并返回新的字符串。

例如:

var arr : string = "回來的"

console.log(arr); //回來的

let a : string = "吳剛"

console.log(arr.concat(a)); //回來的吳剛

4、indexOf()返回某個指定的字符串值在字符串中首次出現(xiàn)的位置。

例如:

var str1 : string = "hellowrold";

console.log(str1.indexOf('ll'))? //2

5、lastIndexOf()從后向前搜索字符串,并從起始位置(0)開始計算返回字符串最后出現(xiàn)的位置。

例如:

var str1 : string = "hellowrold";

console.log(str1.lastIndexOf('o'))? //7

6、localeCompare()用本地特定的順序來比較兩個字符串。

例如:

var str1 = new String( "This is beautiful string" );


var index = str1.localeCompare( "This is beautiful string");?

console.log("localeCompare first :" + index );? //0? 0 - 如果字符串匹配100%。

var str1 = new String( "This is beautiful string" );


var index = str1.localeCompare( "hebeiThis is beautiful string");?

console.log("localeCompare first :" + index );? // 1? ? 1 - 不匹配,參數(shù)值位于區(qū)域設(shè)置排序順序中字符串對象的值之前。

var str1 = new String( "This is beautiful string" );


var index = str1.localeCompare( "This is beautiful stringhebei");?

console.log("localeCompare first :" + index );? ? ? //-1 負(fù)值 - 不匹配,參數(shù)值位于本地排序順序中字符串對象的值之后。

返回值:

0 - 如果字符串匹配100%。

1 - 不匹配,參數(shù)值位于區(qū)域設(shè)置排序順序中字符串對象的值之前。

負(fù)值 - 不匹配,參數(shù)值位于本地排序順序中字符串對象的值之后。

7、match()查找找到一個或多個正則表達(dá)式的匹配。

例如:

let str="1 plus 2 equal 3"

let s = str.match(/\d+/g); //檢索是否有數(shù)字

console.log(s) //1,2,3

8、replace()替換與正則表達(dá)式匹配的子串

例如:

let re = /(\w+)\s(\w+)/;

let str = "han hebei";

let newstr = str.replace(re, "$2, $1");

console.log(newstr);? ? // hebei,han

9、search()檢索與正則表達(dá)式相匹配的值

例如:

let re = /apples/gi;

let str = "Apples are round, and apples are juicy.";

if (str.search(re) == -1 ) {

? console.log("Does not contain Apples" );

} else {

? console.log("Contains Apples" );

} //結(jié)果 Contains Apples

10、split()把字符串分割為子字符串?dāng)?shù)組。

例如:

let str = "hello";

let s = str.split('e');

console.log(s)? //['h','llo']

11、slice()提取字符串的片斷,并在新的字符串中返回被提取的部分。

例如:

let str = "hello";

let s = str.slice(1);

console.log(s)? //ello

12、substr()從起始索引號提取字符串中指定數(shù)目的字符。

例如:

let str="Hello world!"

document.write(str.substr(3))? //lo world

13、substring()提取字符串中兩個指定的索引號之間的字符。

例如:

let str="Hello!"

document.write(str.substring(2,4)) //ll

14、toLocaleLowerCase()根據(jù)主機的語言環(huán)境把字符串轉(zhuǎn)換為小寫,只有幾種語言(如土耳其語)具有地方特有的大小寫映射。

例如:

let str = "Runoob Google";

console.log(str.toLocaleLowerCase( ));? // runoob google

15、toLocaleUpperCase()據(jù)主機的語言環(huán)境把字符串轉(zhuǎn)換為大寫,只有幾種語言(如土耳其語)具有地方特有的大小寫映射。

例如:

let str = "Runoob Google";

console.log(str.toLocaleUpperCase( ));? // RUNOOB GOOGLE

16、toLowerCase()把字符串轉(zhuǎn)換為小寫。

例如:

let str = "Runoob Google";

console.log(str.toLowerCase( ));? // runoob google

17、toString()返回字符串。

例如:

var str = "Runoob";

console.log(str.toString( )); // Runoob

18、toUpperCase()把字符串轉(zhuǎn)換為大寫。

例如:

let str = "hello wrold";

console.log(str.toUpperCase( ));? // HELLO WROLD

19、valueOf()返回指定字符串對象的原始值;當(dāng)調(diào)用該方法的對象不是 String 時拋出 TypeError 異常。。

例如:

var str = new String("Runoob");

console.log(str.valueOf( ));? // Runoob

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容