TypeScript String(字串)
String 對象用於處理文本(字串)。
語法
var txt = new String("string");
或者更簡單方式:
var txt = "string";
String 對象屬性
下表列出了 String 對象支持的屬性:
| 序號 | 屬性 & 描述 | 實例 |
|---|---|---|
| 1. | constructor
對創建該對象的函數的引用。 |
var str = new String( "This is string" );
console.log("str.constructor is:" + str.constructor)
輸出結果: str.constructor is:function String() { [native code] }
|
| 2. | length
返回字串的長度。 |
var uname = new String("Hello World")
console.log("Length "+uname.length) // 輸出 11 |
| 3. | prototype
允許您向對象添加屬性和方法。 |
function employee(id:number,name:string) {
this.id = id
this.name = name
}
var emp = new employee(123,"admin")
employee.prototype.email="admin@xuhuhu.com" // 添加屬性 email
console.log("員工號: "+emp.id)
console.log("員工姓名: "+emp.name)
console.log("員工郵箱: "+emp.email)
|
String 方法
下表列出了 String 對象支持的方法:| 序號 | 方法 & 描述 | 實例 |
|---|---|---|
| 1. | charAt()
返回在指定位置的字元。 |
var str = new String("zaixian");
console.log("str.charAt(0) 為:" + str.charAt(0)); // R
console.log("str.charAt(1) 為:" + str.charAt(1)); // U
console.log("str.charAt(2) 為:" + str.charAt(2)); // N
console.log("str.charAt(3) 為:" + str.charAt(3)); // O
console.log("str.charAt(4) 為:" + str.charAt(4)); // O
console.log("str.charAt(5) 為:" + str.charAt(5)); // B
|
| 2. | charCodeAt()
返回在指定的位置的字元的 Unicode 編碼。 |
var str = new String("zaixian");
console.log("str.charCodeAt(0) 為:" + str.charCodeAt(0)); // 82
console.log("str.charCodeAt(1) 為:" + str.charCodeAt(1)); // 85
console.log("str.charCodeAt(2) 為:" + str.charCodeAt(2)); // 78
console.log("str.charCodeAt(3) 為:" + str.charCodeAt(3)); // 79
console.log("str.charCodeAt(4) 為:" + str.charCodeAt(4)); // 79
console.log("str.charCodeAt(5) 為:" + str.charCodeAt(5)); // 66
|
| 3. | concat()
連接兩個或更多字串,並返回新的字串。 |
var str1 = new String( "zaixian" );
var str2 = new String( "GOOGLE" );
var str3 = str1.concat( str2 );
console.log("str1 + str2 : "+str3) // zaixianGOOGLE
|
| 4. | indexOf()
返回某個指定的字串值在字串中首次出現的位置。 |
var str1 = new String( "zaixian" );
var index = str1.indexOf( "OO" );
console.log("查找的字串位置 :" + index ); // 3
|
| 5. | lastIndexOf()
從後向前搜索字串,並從起始位置(0)開始計算返回字串最後出現的位置。 |
var str1 = new String( "This is string one and again string" );
var index = str1.lastIndexOf( "string" );
console.log("lastIndexOf 查找到的最後字串位置 :" + index ); // 29
index = str1.lastIndexOf( "one" );
console.log("lastIndexOf 查找到的最後字串位置 :" + index ); // 15
|
| 6. | localeCompare()
用本地特定的順序來比較兩個字串。 |
var str1 = new String( "This is beautiful string" );
var index = str1.localeCompare( "This is beautiful string");
console.log("localeCompare first :" + index ); // 0
|
| 7. | match() 查找找到一個或多個正則運算式的匹配。 |
var str="The rain in SPAIN stays mainly in the plain"; var n=str.match(/ain/g); // ain,ain,ain |
| 8. | replace()
替換與正則運算式匹配的子串 |
var re = /(\w+)\s(\w+)/; var str = "zara ali"; var newstr = str.replace(re, "$2, $1"); console.log(newstr); // ali, zara |
| 9. | search()
檢索與正則運算式相匹配的值 |
var re = /apples/gi;
var str = "Apples are round, and apples are juicy.";
if (str.search(re) == -1 ) {
console.log("Does not contain Apples" );
} else {
console.log("Contains Apples" );
}
|
| 10. | slice()
提取字串的片斷,並在新的字串中返回被提取的部分。 |
|
| 11. | split()
把字串分割為子字串數組。 |
var str = "Apples are round, and apples are juicy.";
var splitted = str.split(" ", 3);
console.log(splitted) // [ 'Apples', 'are', 'round,' ]
|
| 12. | substr()
從起始索引號提取字串中指定數目的字元。 |
|
| 13. | substring()
提取字串中兩個指定的索引號之間的字元。 |
var str = "zaixian GOOGLE TAOBAO FACEBOOK";
console.log("(1,2): " + str.substring(1,2)); // U
console.log("(0,10): " + str.substring(0, 10)); // zaixian GOO
console.log("(5): " + str.substring(5)); // B GOOGLE TAOBAO FACEBOOK
|
| 14. | toLocaleLowerCase()
根據主機的語言環境把字串轉換為小寫,只有幾種語言(如土耳其語)具有地方特有的大小寫映射。 |
var str = "zaixian Google"; console.log(str.toLocaleLowerCase( )); // zaixian google |
| 15. | toLocaleUpperCase()
據主機的語言環境把字串轉換為大寫,只有幾種語言(如土耳其語)具有地方特有的大小寫映射。 |
var str = "zaixian Google"; console.log(str.toLocaleUpperCase( )); // zaixian GOOGLE |
| 16. | toLowerCase()
把字串轉換為小寫。 |
var str = "zaixian Google"; console.log(str.toLowerCase( )); // zaixian google |
| 17. | toString()
返回字串。 |
var str = "zaixian"; console.log(str.toString( )); // zaixian |
| 18. | toUpperCase()
把字串轉換為大寫。 |
var str = "zaixian Google"; console.log(str.toUpperCase( )); // zaixian GOOGLE |
| 19. | valueOf()
返回指定字串對象的原始值。 |
var str = new String("zaixian");
console.log(str.valueOf( )); // zaixian
|
