📜  JavaScript 字符串

📅  最后修改于: 2020-09-27 08:40:24             🧑  作者: Mango

在本教程中,您将借助示例学习JavaScript 字符串 。

 

JavaScript 字符串是一种原始数据类型,用于处理文本。例如,

let name = 'John';

创建JavaScript字符串

在JavaScript中, 字符串是通过用引号引起来的。可以使用三种方式使用引号。

  • 单引号: 'Hello'
  • 双引号: "Hello"
  • 反引号: `Hello`

例如,

//strings example
let name = 'Peter';
let name1 = "Jack";
let result = `The names are ${name} and ${name1}`;

单引号和双引号实际上是相同的,您可以使用它们中的任何一个。

当您需要在字符串包含变量或表达式时,通常使用反引号。如上所示,这是通过用${variable or expression}包装变量或表达式来完成的。

您也可以在另一个引号内写一个引号。例如,

let name = 'My name is "Peter".';

但是,报价不应与周围的报价匹配。例如,

let name = 'My name is 'Peter'.'; // error

访问字符串字符

您可以通过两种方式访问字符串中的字符 。

  • 一种方法是将字符串视为数组。例如,
let a = 'hello';
console.log(a[1]); // "e"
  • 另一种方法是使用方法charAt() 。例如,
let a = 'hello';
console.log(a.charAt(1)); // "e"

JavaScript字符串是不可变的

在JavaScript中, 字符串是不可变的。这意味着字符串的字符不能更改。例如,

let a = 'hello';
a[0] = 'H';
console.log(a); // "hello"

但是,您可以将变量名称分配给新的字符串。例如,

let a = 'hello';
a = 'Hello';
console.log(a); // "Hello"

JavaScript区分大小写

JavaScript区分大小写。这意味着在JavaScript中,小写和大写字母被视为不同的值。例如,

let a = 'a';
let b = 'A'
console.log(a === b); // false

 

在JavaScript中, aA被视为不同的值。


JavaScript多行字符串

要使用多行字符串,可以使用+ 运算符或\ 运算符。例如,

// using the + operator
let message1 = 'This is a long message ' + 
'that spans across multiple lines' + 
    'in the code.'

// using the \ operator
let message2 = 'This is a long message \
that spans across multiple lines \
in the code.'

JavaScript字符串长度

要查找字符串的长度,可以使用内置的length属性。例如,

let a = 'hello';
console.log(a.length); // 5

JavaScript字符串对象

您也可以使用new关键字创建字符串 。例如,

let a = 'hello';
let b = new String('hello');

console.log(a); // "hello"
console.log(b); // "hello"

console.log(typeof a); // "string"
console.log(typeof b); // "object"

注意 :建议避免使用字符串对象。使用字符串对象会降低程序速度。


JavaScript字符串方法

以下是常用的JavaScript String方法:

Method Description
charAt(index) returns the character at specified index
concat() joins two or more strings
replace() replaces a string with another string
split() converts the string to an array of strings
substr(start, length) returns a part of a string
substring(start,end) returns a part of a string
slice(start, end) returns a part of a string
toLowerCase() returns the passed string in lower case
toUpperCase() returns the passed string in upper case
trim() removes whitespace from the strings
includes() searches for a string and returns a boolean value
search() searches for a string and returns a position of a match

示例:JavaScript字符串方法

let text1 = 'hello';
let text2 = 'world';
let text3 = '     JavaScript    ';

// concatenating two strings
let result1 = text1.concat(' ', text2);
console.log(result1); // "hello world"

// converting the text to uppercase
let result2 = text1.toUpperCase();
console.log(result2); // HELLO

// removing whitespace from the string
let result3 = text3.trim();
console.log(result3); // JavaScript

// converting the string to an array
let result4 = text1.split();
console.log(result4); // ["hello"]

// slicing the string
let result5= text1.slice(1, 3);
console.log(result5); // "el"

JavaScript String()函数

String() 函数用于将各种数据类型转换为字符串。例如,

let a = 225; // string
let b = true; // boolean

//converting to number
let result1 = String(a);
let result2 = String(b);

console.log(result1); // "225"
console.log(result2); // "true"

如果您想了解有关字符串转换的更多信息,请访问JavaScript Type Conversion。


转义字符

您可以使用反斜杠转义字符 \在字符串包含特殊字符 。例如,

let name = 'My name is \'Peter\'.';
console.log(name);

输出

My name is 'Peter'.

在上面的程序中,使用\包含相同的引号。

这是您可以使用\的其他方式:

Code Output
\” include double quote
\\ include backslash
\n new line
\r carriage return
\v vertical tab
\t horizontal tab
\b backspace
\f form feed