迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

获取 JavaScript 中特定字符后的子字符串

作者:迹忆客 最近更新:2023/02/23 浏览次数:

获取特定字符后的子字符串:

  1. 使用 String.indexOf() 方法获取字符在字符串中的索引。
  2. 使用 String.slice() 方法获取特定字符后的子字符串。
const str = 'abc fql_jiyik.com';

const after_ = str.slice(str.indexOf('_') + 1);

console.log(after_); // 👉️ "jiyik.com"

JavaScript 中特定字符后的子字符串

String.slice() 方法提取字符串的一部分并将其返回,而不修改原始字符串。

String.slice() 方法采用以下参数:

  • start 索引要包含在返回的子字符串中的第一个字符的索引
  • end 索引要从返回的子字符串中排除的第一个字符的索引

当只有一个参数被传递给 String.slice() 方法时,切片会到达字符串的末尾。

const str = 'abc fql_jiyik.com';

console.log(str.slice(10)); // 👉️ jiyik.com

我们使用 String.indexOf 方法获取特定字符第一次出现的索引。

const str = 'abc fql_jiyik.com';

console.log(str.indexOf('_')); // 👉️ 7

console.log(str.indexOf('_') + 1); // 👉️ 8

我们将 1 添加到调用 indexOf() 的结果中,因为我们不想在新字符串中包含该字符。

如果字符串不包含指定字符,则 indexOf() 方法返回 -1。

const str = 'abc fql_jiyik.com';

const result = str.slice(str.indexOf('@') + 1);
console.log(result); // 👉️ abc fql_jiyik.com

如果字符不包含在字符串中,则返回整个字符串。

JavaScript 索引是从零开始的,因此字符串中第一个字符的索引为 0,最后一个字符的索引为 str.length - 1。 定义一个可重用的函数#

如果我们必须经常这样做,请定义一个可重用的函数。

function afterCharacter(string, char) {
  return string.slice(str.indexOf(char) + 1);
}

const str = 'abc fql_jiyik.com';

// 👇️ jiyik.com
console.log(afterCharacter(str, '_'));

// 👇️ fql_jiyik.com
console.log(afterCharacter(str, ' '));

afterCharacter 函数接受一个字符串和一个字符作为参数,返回指定字符之后的字符串部分。

或者,我们可以使用 str.split() 方法。


使用 String.split() 获取特定字符后的子字符串

获取特定字符后的子字符串:

  1. 使用 split() 方法在字符上拆分字符串。
  2. 访问索引为 1 的字符串数组。
  3. 数组中的第一个元素是字符之后的子字符串。
const str = 'abc fql_jiyik.com';

const after_ = str.split('_')[1];

console.log(after_); // 👉️ jiyik.com

我们使用 String.split() 方法在每次出现下划线时拆分字符串。

const str = 'abc bobby_hadz.com';

// 👇️ [ 'abc bobby', 'hadz.com' ]
console.log(str.split('_'));

数组中的第二个元素是指定字符(示例中的下划线)之后的子字符串。


处理字符不在字符串中的场景

如果在字符串中找不到分隔符,则 split() 方法返回一个仅包含 1 个元素的数组,该元素由整个字符串组成。

const str = 'abc fql_jiyik.com';

// 👇️ [ 'abc fql_jiyik.com' ]
console.log(str.split('@'));

如果我们访问数组中的第二个元素,我们将得到一个未定义的值。

处理此问题的一种方法是,如果未找到分隔符,则使用逻辑 OR || 运算符返回整个字符串。

const str = 'abc fql_jiyik.com';

const after_ = str.split('@')[1] || str;

console.log(after_); // 👉️ abc fql_jiyik.com

我们使用逻辑 OR || 运算符来提供后备值,以防运算符左侧的值是假的(例如未定义)。

如果分隔符不包含在字符串中,则表达式返回整个字符串。


定义一个使用 split() 的可重用函数

如果我们必须经常这样做,请定义一个可重用的函数。

function afterCharacter(string, char) {
  return string.split(char)[1] || string;
}

const str = 'abc fql_jiyik.com';

// 👇️ jiyik.com
console.log(afterCharacter(str, '_'));

// 👇️ fql_jiyik.com
console.log(afterCharacter(str, ' '));

// 👇️ abc fql_jiyik.com
console.log(afterCharacter(str, '#'));

该函数接受一个字符串和一个字符作为参数,返回指定字符之后的字符串部分。

我们还可以使用 Array.pop() 方法代替字符串索引。

const str = 'abc fql_jiyik.com';

const after_ = str.split('_').pop();

console.log(after_); // 👉️ jiyik.com

我们使用 String.split() 方法在字符上拆分字符串。

const str = 'abc fql_jiyik.com';

console.log(str.split('_')); // 👉️ [ 'abc fql', 'jiyik.com' ]

最后一步是使用 Array.pop() 方法获取数组的最后一个元素。

Array.pop() 方法移除并返回最后一个数组元素。


如果字符串包含多次出现的字符,请不要使用 split()

请注意 ,如果字符串包含多次出现的字符,则不应使用 String.split() 方法。

const str = 'A very_long_string';

// 👇️ [ 'A very', 'long', 'string' ]
console.log(str.split('_'));

const after_ = str.split('_').pop();

console.log(after_); // 👉️ string

String.split() 方法在所有出现的字符处拆分字符串,因此访问最后一个数组元素不再是一个选项。

在这种情况下,我们应该使用前面副标题中所示的 String.slice() 方法。

const str = 'abc fql_jiyik.com';

const after_ = str.slice(str.indexOf('_') + 1);

console.log(after_); // 👉️ "jiyik.com"

indexOf() 方法返回字符第一次出现的索引,我们使用 String.slice() 方法获取字符之后的字符串切片。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

How much do you know about the Prototype Chain?

发布时间:2025/02/21 浏览次数:150 分类:JavaScript

The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便