如何在TypeScript中查詢字串的長度?
字串可以包含一系列重複和非重複的字元。字串的長度是指字串中包含的字元總數,包括特殊字元、字母字元等。
在這裡,我們將學習在TypeScript中計算字串長度的不同方法。
使用字串的length屬性
在TypeScript中,字串是一個庫,或者可以說它是一個類。它包含一些屬性和方法,我們可以透過將字串類物件作為引用來呼叫它們。length也是字串類的一個屬性,它返回字串中字元的總數。
語法
使用者可以按照以下語法使用字串類的length屬性來查詢字串的長度。
let string1: string = "TutorialsPoint"; let len = string1.length;
示例
在下面的示例中,我們使用了不同型別的字串,並使用length屬性來查詢它們的長度。第一個字串是一個沒有特殊字元的普通字串,第二個字串包含特殊字元,第三個字串包含空格。
使用者可以在輸出中觀察到,length屬性將空格計算為字串字元,並相應地返回字串長度。
// Regular string
let string1: string = "TutorialsPoint";
let len = string1.length;
console.log("The length of `" + string1 + "` is " + len);
// string containing the special characters
string1 = "Tutor!@l$Po!nT";
len = string1.length;
console.log("The length of `" + string1 + "` is " + len);
// String containing spaces.
string1 = "Welcome to the TutorialsPoint!";
len = string1.length;
console.log("The length of `" + string1 + "` is " + len);
編譯後,它將生成以下JavaScript程式碼:
// Regular string
var string1 = "TutorialsPoint";
var len = string1.length;
console.log("The length of `" + string1 + "` is " + len);
// string containing the special characters
string1 = "Tutor!@l$Po!nT";
len = string1.length;
console.log("The length of `" + string1 + "` is " + len);
// String containing spaces.
string1 = "Welcome to the TutorialsPoint!";
len = string1.length;
console.log("The length of `" + string1 + "` is " + len);
輸出
以上程式碼將產生以下輸出:
The length of `TutorialsPoint` is 14 The length of `Tutor!@l$Po!nT` is 14 The length of `Welcome to the TutorialsPoint!` is 30
使用for迴圈計算字串長度
使用者還可以建立一個自定義函式來查詢TypeScript中字串的長度。對於初學者來說,查詢字串長度的自定義函式很有用,因為面試官可能會要求在不使用length屬性的情況下查詢字串的長度。
語法
使用者可以按照以下語法使用for迴圈來查詢字串的長度。
let string1: string = "TutorialsPoint";
let len = 0;
for (let char of string1) {
len++;
}
示例
在下面的示例中,我們建立了字串變數'str'和數字變數'len'。之後,我們使用for...of迴圈迭代字串的每個字元。在使用for...of迴圈迭代字串時,我們為字串的每個字元向len變數新增1。
最後,我們將len變數的最終值作為引數化字串的長度從get_len()函式返回。
// function to find the length of the string
function get_len(str: string): number {
// Initalize the len variable with zero
let len: number = 0;
// Iterate through the every character of the string,
// and for every character add 1 to the len variable.
for (let char of str) {
len++;
}
// return len variable
return len;
}
let my_name: string = "Shubham";
// call the get_len() function
let name_len: number = get_len(my_name);
// Print the string.
console.log("The length of `" + my_name+ "` is " + name_len);
編譯後,它將生成以下JavaScript程式碼:
// function to find the length of the string
function get_len(str) {
// Initalize the len variable with zero
var len = 0;
// Iterate through the every character of the string,
// and for every character add 1 to the len variable.
for (var _i = 0, str_1 = str; _i < str_1.length; _i++) {
var char = str_1[_i];
len++;
}
// return len variable
return len;
}
var my_name = "Shubham";
// call the get_len() function
var name_len = get_len(my_name);
// Print the string.
console.log("The length of `" + my_name + "` is " + name_len);
輸出
以上程式碼將產生以下輸出:
The length of `Shubham` is 7
示例
我們在下面給出的示例中修改了上面的示例。我們只計算字串中字母字元的總數。我們使用了字串類的charCodeAt()方法來獲取字元的ASCII值,並根據字元的ASCII值從字串長度中排除字元。
在輸出中,使用者可以觀察到get_len()函式在從字串中排除特殊字元後返回字串長度。
function get_len(str: string): number {
let len: number = 0;
for (let i = 0; i < str.length; i++) {
if (
(str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) ||
(str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122)
) {
len++;
}
}
return len;
}
let test: string = "RBKQW ASNQW @!@#!@";
console.log("The length of `" + test + "` excluding the special characters is " + get_len(test));
test = "TutorialsPoint";
console.log("The length of `" + test + "` excluding the special characters is " + get_len(test));
編譯後,它將生成以下JavaScript程式碼:
function get_len(str) {
var len = 0;
for (var i = 0; i < str.length; i++) {
if ((str.charCodeAt(i) >= 65 && str.charCodeAt(i) <= 90) ||
(str.charCodeAt(i) >= 97 && str.charCodeAt(i) <= 122)) {
len++;
}
}
return len;
}
var test = "RBKQW ASNQW @!@#!@";
console.log("The length of `" + test + "` excluding the special characters is " + get_len(test));
test = "TutorialsPoint";
console.log("The length of `" + test + "` excluding the special characters is " + get_len(test));
輸出
以上程式碼將產生以下輸出:
The length of `RBKQW ASNQW @!@#!@` excluding the special characters is 10 The length of `TutorialsPoint` excluding the special characters is 14
我們學習了兩種不同的查詢字串長度的方法。在第一種方法中,我們簡單地使用了字串類的length屬性。在第二種方法中,我們建立了自定義函式並修改了示例以從字串中排除特殊字元。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP