JavaScript 字符串
                           
天天向上
发布: 2025-02-25 22:35:32

原创
19 人浏览过

在 JavaScript 中,字符串(String) 是一组字符的集合,可以用来表示文本数据。字符串是常用的数据类型之一,JavaScript 提供了许多内置方法来操作字符串。

1. 创建字符串

a. 使用单引号或双引号

let str1 = 'Hello, World!';  // 使用单引号
let str2 = "Hello, World!";  // 使用双引号

b. 使用模板字面量(Template Literals)

模板字面量用反引号(`)包裹,可以在字符串中嵌入变量或表达式,支持多行字符串。

let name = "Alice";
let greeting = `Hello, ${name}!`;  // 插值
console.log(greeting);  // 输出: Hello, Alice!

let multiLineString = `This is
a multi-line
string.`;
console.log(multiLineString);  // 输出: This is
                                //       a multi-line
                                //       string.

2. 字符串的常用方法

a. 字符串长度:使用 .length 属性可以获取字符串的长度。

let str = "Hello, World!";
console.log(str.length);  // 输出: 13

b. 访问字符:通过索引访问字符串中的字符。索引从 0 开始。

let str = "Hello";
console.log(str[0]);  // 输出: H
console.log(str[4]);  // 输出: o

c. 字符串拼接:使用 +.concat() 方法连接字符串。

let str1 = "Hello";
let str2 = "World!";
let result = str1 + " " + str2;  // 使用 + 拼接
console.log(result);  // 输出: Hello World!

// 使用 concat() 拼接
let result2 = str1.concat(" ", str2);
console.log(result2);  // 输出: Hello World!

d. 查找子字符串

  • indexOf():查找子字符串第一次出现的位置。
  • lastIndexOf():查找子字符串最后一次出现的位置。
let str = "Hello, World!";
console.log(str.indexOf("World"));  // 输出: 7
console.log(str.lastIndexOf("o"));  // 输出: 8

e. 提取子字符串:使用 slice()substring()substr() 提取部分字符串。

let str = "Hello, World!";
console.log(str.slice(0, 5));  // 输出: Hello
console.log(str.substring(7, 12));  // 输出: World
console.log(str.substr(7, 5));  // 输出: World

f. 转换大小写

let str = "Hello, World!";
console.log(str.toUpperCase());  // 输出: HELLO, WORLD!
console.log(str.toLowerCase());  // 输出: hello, world!

g. 替换内容:使用 replace() 替换字符串中的部分内容。

let str = "Hello, World!";
let newStr = str.replace("World", "JavaScript");
console.log(newStr);  // 输出: Hello, JavaScript!

h. 分割字符串:使用 split() 方法将字符串按指定的分隔符拆分成数组。

let str = "apple,banana,orange";
let fruits = str.split(",");
console.log(fruits);  // 输出: ["apple", "banana", "orange"]

i. 去除空白字符

  • trim():去除字符串两端的空白字符。
  • trimStart()trimEnd():分别去除字符串开始和结束的空白字符。
let str = "  Hello, World!  ";
console.log(str.trim());  // 输出: Hello, World!

j. 字符替换与正则表达式

let str = "Hello 123, Hello 456!";
let result = str.replace(/\d+/g, "*");
console.log(result);  // 输出: Hello *, Hello *!

3. 字符串模板与表达式

模板字面量(Template Literals)使得插入变量或表达式变得非常简便。

let name = "John";
let age = 30;
let introduction = `My name is ${name}, and I am ${age} years old.`;
console.log(introduction);  // 输出: My name is John, and I am 30 years old.

4. 多行字符串

使用模板字面量可以很容易地创建多行字符串。

let text = `This is a multi-line string.
It can span across multiple lines.
Just like this one!`;
console.log(text);
// 输出:
// This is a multi-line string.
// It can span across multiple lines.
// Just like this one!

5. 编码与解码

JavaScript 提供了 encodeURIComponent()decodeURIComponent() 函数来对 URL 中的字符串进行编码和解码。

let url = "https://www.example.com?search=JavaScript & more";
let encodedUrl = encodeURIComponent(url);
console.log(encodedUrl);  // 输出: https%3A%2F%2Fwww.example.com%3Fsearch%3DJavaScript%20%26%20more

let decodedUrl = decodeURIComponent(encodedUrl);
console.log(decodedUrl);  // 输出: https://www.example.com?search=JavaScript & more

6. 字符串的不可变性

JavaScript 中的字符串是不可变的,这意味着一旦创建了字符串,其内容不能被改变。每次对字符串进行修改操作时,都会创建一个新的字符串。例如:

let str = "Hello";
str[0] = "h";  // 无效
console.log(str);  // 输出: Hello

总结

  • 字符串是 JavaScript 中常用的数据类型,可以通过不同方式创建和操作。
  • JavaScript 提供了多种方法来处理字符串,如查找、替换、拼接、大小写转换、提取子字符串等。
  • 字符串是不可变的,即无法直接改变已有字符串的内容。

更多详细内容请关注其他文章!

发表回复 0

Your email address will not be published. Required fields are marked *