
- TypeScript 基礎
- TypeScript - 首頁
- TypeScript - 路線圖
- TypeScript - 概述
- TypeScript - 環境搭建
- TypeScript - 基本語法
- TypeScript vs. JavaScript
- TypeScript - 特性
- TypeScript - 變數
- TypeScript - let & const
- TypeScript - 運算子
- TypeScript 基本型別
- TypeScript - 型別
- TypeScript - 型別註解
- TypeScript - 型別推斷
- TypeScript - 數字
- TypeScript - 字串
- TypeScript - 布林值
- TypeScript - 陣列
- TypeScript - 元組
- TypeScript - 列舉
- TypeScript - any
- TypeScript - never
- TypeScript - 聯合型別
- TypeScript - 字面量型別
- TypeScript - 符號
- TypeScript - null vs. undefined
- TypeScript - 類型別名
- TypeScript 控制流
- TypeScript - 決策
- TypeScript - if 語句
- TypeScript - if else 語句
- TypeScript - 巢狀 if 語句
- TypeScript - switch 語句
- TypeScript - 迴圈
- TypeScript - for 迴圈
- TypeScript - while 迴圈
- TypeScript - do while 迴圈
- TypeScript 函式
- TypeScript - 函式
- TypeScript - 函式型別
- TypeScript - 可選引數
- TypeScript - 預設引數
- TypeScript - 匿名函式
- TypeScript - 函式構造器
- TypeScript - rest 引數
- TypeScript - 引數解構
- TypeScript - 箭頭函式
- TypeScript 介面
- TypeScript - 介面
- TypeScript - 介面擴充套件
- TypeScript 類和物件
- TypeScript - 類
- TypeScript - 物件
- TypeScript - 訪問修飾符
- TypeScript - 只讀屬性
- TypeScript - 繼承
- TypeScript - 靜態方法和屬性
- TypeScript - 抽象類
- TypeScript - 訪問器
- TypeScript - 鴨子型別
- TypeScript 高階型別
- TypeScript - 交叉型別
- TypeScript - 型別守衛
- TypeScript - 型別斷言
- TypeScript 型別操作
- TypeScript - 從型別建立型別
- TypeScript - keyof 型別運算子
- TypeScript - typeof 型別運算子
- TypeScript - 索引訪問型別
- TypeScript - 條件型別
- TypeScript - 對映型別
- TypeScript - 模板字面量型別
- TypeScript 泛型
- TypeScript - 泛型
- TypeScript - 泛型約束
- TypeScript - 泛型介面
- TypeScript - 泛型類
- TypeScript 其他
- TypeScript - 三斜槓指令
- TypeScript - 名稱空間
- TypeScript - 模組
- TypeScript - 環境宣告
- TypeScript - 裝飾器
- TypeScript - 型別相容性
- TypeScript - Date 物件
- TypeScript - 迭代器和生成器
- TypeScript - Mixin
- TypeScript - 實用型別
- TypeScript - 裝箱和拆箱
- TypeScript - tsconfig.json
- 從 JavaScript 到 TypeScript
- TypeScript 有用資源
- TypeScript - 快速指南
- TypeScript - 有用資源
- TypeScript - 討論
TypeScript - 字串
在 TypeScript 中,字串表示一系列字元。字串型別是 TypeScript 中的基本資料型別。字串用於儲存可以以文字形式表示的資料。與 JavaScript 一樣,TypeScript 也支援字串原生型別和 String 物件。
String 物件允許您處理一系列字元。它用許多輔助方法包裝字串原生資料型別。
您可以呼叫原生字串上的方法,因為 TypeScript 會自動包裝字串原生型別並在包裝器物件上呼叫方法。屬性也適用相同規則。
建立字串
TypeScript 中的字串可以作為來自字串字面量的原生型別建立,也可以使用 String() 建構函式作為物件建立。
您可以使用以下語法在 TypeScript 中建立一個String 物件:
cost str = new String(value);
這裡,str 是新建立的 String 物件,value 是一系列字元。
您可以使用單引號、雙引號和反引號建立字串原生型別。
let str1: string = 'a string primitive'; let str2: string = "another string"; let str3: string = `yet another string`;
字串原生型別也可以使用不帶 new 關鍵字的 String() 函式建立。
let str: string = String(value);
‘string’ 是原生型別,但 ‘String’ 是包裝器物件。儘可能優先使用 ‘string’。
字串屬性
以下是 String 物件中可用方法及其說明的列表:
序號 | 屬性 & 說明 |
---|---|
1. | constructor
返回建立物件的 String 函式的引用。 |
2. | length
返回字串的長度。 |
3. | prototype
prototype 屬性允許您向物件新增屬性和方法。 |
字串方法
以下是 String 物件中可用方法及其說明的列表:
序號 | 方法 & 說明 |
---|---|
1. | charAt()
返回指定索引處的字元。 |
2. | charCodeAt()
返回一個數字,指示給定索引處字元的 Unicode 值。 |
3. | concat()
組合兩個字串的文字並返回一個新字串。 |
4. | indexOf()
返回呼叫 String 物件中指定值第一次出現的索引,如果未找到則返回 -1。 |
5. | lastIndexOf()
返回呼叫 String 物件中指定值最後一次出現的索引,如果未找到則返回 -1。 |
6. | localeCompare()
返回一個數字,指示引用字串在排序順序中是在給定字串之前、之後還是與之相同。 |
7. | match() 用於將正則表示式與字串匹配。 |
8. | replace()
用於查詢正則表示式和字串之間的匹配項,並將匹配的子字串替換為新的子字串。 |
9. | search()
執行在正則表示式和指定字串之間查詢匹配項的操作。 |
10. | slice()
提取字串的一部分並返回一個新字串。 |
11. | split()
透過將字串分割成子字串,將 String 物件分割成字串陣列。 |
12. | substr()
返回字串中從指定位置開始的指定數量的字元。 |
13. | substring()
返回字串中兩個索引之間的字元。 |
14. | toLocaleLowerCase()
字串中的字元轉換為小寫,同時尊重當前區域設定。 |
15. | toLocaleUpperCase()
字串中的字元轉換為大寫,同時尊重當前區域設定。 |
16. | toLowerCase()
返回轉換為小寫的呼叫字串值。 |
17. | toString()
返回表示指定物件的字串。 |
18. | toUpperCase()
返回轉換為大寫的呼叫字串值。 |
19. | valueOf()
返回指定物件的原生值。 |
示例
讓我們藉助 TypeScript 中的一些示例來了解字串型別。
示例:建立字串原生型別
在下面的示例中,我們分別使用單引號、雙引號和反引號建立原生字串 str1、str2 和 str3。
let str1: string = 'a string primitive'; console.log(str1); let str2: string = "another string"; console.log(str2); let str3: string = `yet another string`; console.log(str3);
編譯後,它將生成以下 JavaScript 程式碼。
let str1 = 'a string primitive'; console.log(str1); let str2 = "another string"; console.log(str2); let str3 = `yet another string`; console.log(str3);
上述示例程式碼的輸出如下:
a string primitive another string yet another string
示例:建立字串物件
這裡我們使用帶有 new 關鍵字的 String() 建構函式建立一個 String 物件。
const email = new String('example133@gmail.com'); console.log(email); console.log(typeof email);
編譯後,它將生成以下 JavaScript 程式碼。
const email = new String('example133@gmail.com'); console.log(email); console.log(typeof email);
上述示例程式碼的輸出如下:
example133@gmail.com object
示例:連線 TypeScript 字串
要連線兩個字串,我們可以使用 + 運算子。在這裡,我們連線兩個字串 str1 和 str2,並在控制檯中顯示結果。
let str1: string = "Hello "; let str2: string = "World!"; let str3: string = str1 + str2; console.log(str3);
編譯後,它將生成以下 JavaScript 程式碼。
let str1 = "Hello "; let str2 = "World!"; let str3 = str1 + str2; console.log(str3);
上述示例程式碼的輸出如下:
Hello World!
示例:使用索引訪問字串元素
在下面的示例中,我們訪問字串中第 1 個和第 6 個索引的字元。
let message: string = "Hello World!"; console.log("Character at index 1 is => " + message[1]); console.log("Character at index 6 is => " + message[6]);
編譯後,它將生成以下 JavaScript 程式碼。
let message = "Hello World!"; console.log("Character at index 1 is => " + message[1]); console.log("Character at index 6 is => " + message[6]);
上述示例程式碼的輸出如下:
Character at index 1 is => e Character at index 6 is => W
示例:TypeScript 中的 String 與 string
在 TypeScript 中,型別 'String' 是一個包裝器物件,型別 'string' 是一個原生型別。這兩種型別不能相互賦值。
在下面的示例中,我們嘗試將一個字串物件賦值給 string 原生型別的變數。
let str: string; str = new String('shahid');
上述示例程式碼將顯示以下錯誤:
Type 'String' is not assignable to type 'string'. 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.
始終建議使用字串原生型別。