文章目录
在本教程中,您将借助示例了解 JavaScript 字符串。
JavaScript 字符串是一种用于处理文本的原始数据类型。例如,
const name = 'John';
创建 JavaScript 字符串
在 JavaScript 中,字符串是用引号括起来的。有三种方法使用引号。
- 单引号: ‘Hello’
- 双引号: “Hello”
- 反引号: `Hello`
例如,
//strings example
const name = 'Peter';
const name1 = "Jack";
const result = `The names are ${name} and ${name1}`;
单引号和双引号实际上是相同的,您可以使用它们中的任何一个。
当需要将变量或表达式包含到字符串中时,通常使用反引号。这是通过使用 $ { variable 或 expression } 包装变量或表达式来实现的,如上所示。
您也可以在一个引号中写另一个引号。例如,
const name = 'My name is "Peter".';
但是,引号不应与周围的引号一致。例如,
const name = 'My name is 'Peter'.'; // error
访问字符串字符
您可以通过两种方式访问字符串中的字符。
- 一种方式是将字符串视为数组。例如,
const a = 'hello';
console.log(a[1]); // "e"
- 另一种方法是使用方法 charAt()。例如,
const 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 中,小写和大写字母被视为不同的值。例如,
const a = 'a';
const b = 'A'
console.log(a === b); // false
在 JavaScript 中,a 和 A 被视为不同的值。
JavaScript 多行字符串
要使用多行字符串,可以使用 + 运算符或 \ 运算符。例如,
// using the + operator
const message1 = 'This is a long message ' +
'that spans across multiple lines' +
'in the code.'
// using the \ operator
const message2 = 'This is a long message \
that spans across multiple lines \
in the code.'
JavaScript 字符串长度
要查找字符串的长度,可以使用内置的 length 属性。例如,
const a = 'hello';
console.log(a.length); // 5
JavaScript 字符串对象
您还可以使用 new 关键字创建字符串。例如,
const a = 'hello';
const b = new String('hello');
console.log(a); // "hello"
console.log(b); // "hello"
console.log(typeof a); // "string"
console.log(typeof b); // "object"
注意:建议避免使用字符串对象。使用字符串对象会减慢程序的速度。
JavaScript 字符串方法
以下是常用的 JavaScript 字符串方法:
| 方法 | 描述 |
|---|---|
| charAt(index) | 返回指定索引处的字符 |
| concat() | 连接两个或多个字符串 |
| replace() | 用一个字符串替换另一个字符串 |
| split() | 将字符串转换为字符串数组 |
| substr(start, length) | 返回字符串的一部分 |
| substring(start,end) | 返回字符串的一部分 |
| slice(start, end) | 返回字符串的一部分 |
| toLowerCase() | 以小写形式返回传递的字符串 |
| toUpperCase() | 以大写形式返回传递的字符串 |
| trim() | 删除字符串中的空白 |
| includes() | 搜索字符串并返回布尔值 |
| search() | 搜索字符串并返回匹配的位置 |
示例:JavaScript 字符串方法
const text1 = 'hello';
const text2 = 'world';
const text3 = ' JavaScript ';
// concatenating two strings
const result1 = text1.concat(' ', text2);
console.log(result1); // "hello world"
// converting the text to uppercase
const result2 = text1.toUpperCase();
console.log(result2); // HELLO
// removing whitespace from the string
const result3 = text3.trim();
console.log(result3); // JavaScript
// converting the string to an array
const result4 = text1.split();
console.log(result4); // ["hello"]
// slicing the string
const result5= text1.slice(1, 3);
console.log(result5); // "el"
JavaScript String() 函数
String() 函数用于将各种数据类型转换为字符串。例如,
const a = 225; // number
const b = true; // boolean
//converting to string
const result1 = String(a);
const result2 = String(b);
console.log(result1); // "225"
console.log(result2); // "true"
如果您想了解有关字符串转换的更多信息,请访问 JavaScript 类型转换。
转义字符
可以使用反斜杠转义字符 \ 在字符串中包含特殊字符。例如,
const name = 'My name is \'Peter\'.';
console.log(name);
输出
My name is 'Peter'.
在上述程序中,使用引号时需要配置 \ 。
| 代码 | 输出 |
|---|---|
| \" | 包括双引号 |
| \\ | 包括反斜杠 |
| \n | 换行 |
| \r | 回车 |
| \v | 垂直制表符 |
| \t | 水平制表符 |
| \b | 退格 |
| \f | 换页 |
上一教程 :Multidimensional Array 下一教程 :JS for…in
参考文档
[1] Parewa Labs Pvt. Ltd. (2022, January 1). Getting Started With JavaScript, from Parewa Labs Pvt. Ltd: https://www.programiz.com/javascript/string










