如何在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