如何在 TypeScript 中查找字符串的长度?
字符串可以包含重复和非重复字符的序列。字符串的长度表示字符串在字符序列中包含的字符总数,包括特殊字符、字母字符等。
在这里,我们将学习在 TypeScript 中计算字符串长度的不同方法。
使用字符串的 length 属性
在 TypeScript 中,字符串是一个库,或者我们可以说它是一个类。它包含一些属性和方法,我们可以通过以字符串类对象为引用来调用它们。 length 也是 string 类的属性,它返回字符串中的字符总数。
语法
用户可以按照以下语法使用 string 类的 length 属性来查找字符串的长度。
let string1: string = "TutorialsPoint"; let len = string1.length;
示例
在下面的示例中,我们采用了不同类型的字符串,并使用 length 属性来查找它们的长度。第一个字符串是没有特殊字符的常规字符串,第二个字符串包含特殊字符,第三个字符串包含空格。
用户可以在输出中观察到 length 属性将空格计为字符串字符并相应地返回字符串长度。
// 常规字符串 let string1: string = "TutorialsPoint"; let len = string1.length; console.log("The length of `" + string1 + "` is " + len); // 包含特殊字符的字符串 string1 = "Tutor!@l$Po!nT"; len = string1.length; console.log("The length of `" + string1 + "` is " + len); // 包含空格的字符串。 string1 = "Welcome to the TutorialsPoint!"; len = string1.length; console.log("The length of `" + string1 + "` is " + len);
编译后,它将生成以下 JavaScript 代码 -
// 常规字符串 var string1 = "TutorialsPoint"; var len = string1.length; console.log("The length of `" + string1 + "` is " + len); // 包含特殊字符的字符串 string1 = "Tutor!@l$Po!nT"; len = string1.length; console.log("The length of `" + string1 + "` is " + len); // 包含空格的字符串。 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 get_len(str: string): number { // 用零初始化 len 变量 let len: number = 0; // 遍历字符串的每个字符, // 并为每个字符将 len 变量加 1。 for (let char of str) { len++; } // 返回 len 变量 return len; } let my_name: string = "Shubham"; // 调用 get_len() 函数 let name_len: number = get_len(my_name); // 打印字符串。 console.log("The length of `" + my_name+ "` is " + name_len);
编译后,它将生成以下 JavaScript 代码 -
// 函数用于查找字符串的长度 function get_len(str) { // 用零初始化 len 变量 var len = 0; // 遍历字符串的每个字符, // 并为每个字符将 len 变量加 1。 for (var _i = 0, str_1 = str; _i < str_1.length; _i++) { var char = str_1[_i]; len++; } // 返回 len 变量 return len; } var my_name = "Shubham"; // 调用 get_len() 函数 var name_len = get_len(my_name); // 打印字符串。 console.log("The length of `" + my_name + "` is " + name_len);
输出
上述代码将产生以下输出 -
The length of `Shubham` is 7
示例
我们在下面给出的示例中对上述示例进行了处理。我们仅计算字符串中的字母字符总数。我们使用了字符串类的 chareCodeAt() 方法获取字符的 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
我们学习了两种不同的方法来查找字符串的长度。在第一种方法中,我们仅使用字符串类的长度属性。在第二种方法中,我们创建了自定义函数并操纵示例以从字符串中排除特殊字符。