📜  TypeScript字符串

📅  最后修改于: 2021-01-11 12:29:57             🧑  作者: Mango

TypeScript字符串

在TypeScript中,字符串是一个代表字符值序列的对象。它是一种原始数据类型,用于存储文本数据。字符串值用单引号或双引号引起来。字符数组的工作原理与字符串相同。

句法

let var_name = new String(string);

let uname = new String("Hello JavaTpoint");
console.log("Message: " +uname);
console.log("Length: "+uname.length);

输出:

Message: Hello JavaTpoint
Length: 16

我们可以通过三种方式创建字符串。

1.单引号字符串

它将字符串括在单引号中,如下所示。

var studentName: String = 'Peter';

2.双引号字符串

它将字符串用双引号引起来,如下所示。

var studentName: String = "Peter";

3.反勾字符串

它用于编写表达式。我们可以使用它将表达式嵌入到字符串。也称为模板字符串。 TypeScript支持ES6版本中的模板字符串。

let empName:string = "Rohit Sharma"; 
let compName:string = "JavaTpoint"; 
// Pre-ES6
let empDetail1: string = empName + " works in the " + compName + " company."; 
// Post-ES6
let empDetail2: string = `${empName} works in the ${compName} company.`; 
console.log("Before ES6: " +empDetail1);
console.log("After ES6: " +empDetail2);

输出:

Before ES6: Rohit Sharma works in the JavaTpoint company.
After ES6: Rohit Sharma works in the JavaTpoint company.

多行字符串

ES6提供了我们编写多行字符串。我们可以从以下示例中了解它。

let multi = 'hello ' +
    'world ' +
    'my ' +
    'name ' +
    'is ' +
    'Rohit';

如果我们希望字符串中的每一行都包含“换行”字符,则必须在每个字符串的末尾添加“ \ n”

let multi = ' hello\n ' +
    'JavaTpoint\n ' +
    'my\n ' +
    'name\n ' +
    'is\n ' +
    'Rohit Sharma';
console.log(multi);

输出:

hello
JavaTpoint
my
name
is
Rohit Sharma

字符串字面量类型

字符串字面量是用双引号(“”)括起来的一系列字符。它用来表示一个字符序列,该字符序列形成一个以空字符结尾的字符串。它允许我们指定在“字符串字面量类型”中指定的确切字符串值。它在不同的字符串值之间使用“管道”或“ |”符号。

句法

Type variableName = "value1" | "value2" | "value3"; // upto N number of values

字符串字面量有两种使用方式:

1.变量分配

我们只能将允许的值分配给字面量类型变量。否则,将给出编译时错误。

type Pet = 'cat' | 'dog' | 'Rabbit';
let pet: Pet;
if(pet = 'cat'){
    console.log("Correct");
};
if(pet = 'Deer')
{
    console.log("compilation error");
};

输出:

Correct
compilation error

2.功能参数

我们只能将定义的值传递给字面量类型参数。否则,将给出编译时错误。

type FruitsName = "Apple" | "Mango" | "Orange";
function showFruitName(fruitsName: FruitsName): void {
    console.log(fruitsName);
}
showFruitName('Mango');   //OK - Print 'Mango'
//Compile Time Error
showFruitName('Banana');

输出:

Mango
Banana

字符串方法

字符串方法及其说明的列表如下。

SN Method Description
1. charAt() It returns the character of the given index.
2. concat() It returns the combined result of two or more string.
3. endsWith() It is used to check whether a string ends with another string.
4. includes() It checks whether the string contains another string or not.
5. indexOf() It returns the index of the first occurrence of the specified substring from a string, otherwise returns -1.
6. lastIndexOf() It returns the index of the last occurrence of a value in the string.
7. match() It is used to match a regular expression against the given string.
8. replace() It replaces the matched substring with the new substring.
9. search() It searches for a match between a regular expression and string.
10. slice() It returns a section of a string.
11. split() It splits the string into substrings and returns an array.
12. substring() It returns a string between the two given indexes.
13. toLowerCase() It converts the all characters of a string into lower case.
14. toUpperCase() It converts the all characters of a string into upper case.
15. trim() It is used to trims the white space from the beginning and end of the string.
16. trimLeft() It is used to trims the white space from the left side of the string.
17. trimRight() It is used to trims the white space from the right side of the string.
18. valueOf() It returns a primitive value of the specified object.

//String Initialization
let str1: string = 'Hello';
let str2: string = 'JavaTpoint';
//String Concatenation
console.log("Combined Result: " +str1.concat(str2));
//String charAt
console.log("Character At 4: " +str2.charAt(4));
//String indexOf
console.log("Index of T: " +str2.indexOf('T'));
//String replace
console.log("After Replacement: " +str1.replace('Hello', 'Welcome to'));
//String uppercase
console.log("UpperCase: " +str2.toUpperCase());

输出:

Combined Result: HelloJavaTpoint
Character At 4: T
Index of T: 4
After Replacement: Welcome to
UpperCase: JAVATPOINT