在 JavaScript 中替换字符串中最后一次出现的字符

目录

Replace Last Occurrence of Character in String in JavaScript

  1. 在 Javascript 中替换字符串中最后一次出现的字符
  2. 使用 split() 替换字符串中最后一次出现的字符
  3. 使用 substring() 替换字符串中最后一次出现的字符
  4. JS去除字符串中最后一次出现的字符

在 Javascript 中替换字符串中最后一次出现的字符

要替换字符串中最后一次出现的字符:

  1. 使用该lastIndexOf()方法获取字符的最后一个索引。
  2. 两次使用该slice()方法获取字符前后的部分。
  3. 在字符串的两部分之间添加替换字符串。
索引.js
const str = 'bobbyhadz.com'; const lastIndex = str.lastIndexOf('b'); const replacement = '.'; const replaced = str.slice(0, lastIndex) + replacement + str.slice(lastIndex + 1); console.log(replaced); // 👉️ bob.yhadz.com
如果您需要替换最后一次出现的子字符串,请使用替换字符串的长度来确定第二次调用该方法时的起始索引 slice()
索引.js
const str = 'bbobbyhadz.com'; const lastIndex = str.lastIndexOf('bb'); const replacement = '..'; const replaced = str.slice(0, lastIndex) + replacement + str.slice(lastIndex + replacement.length); console.log(replaced); // 👉️ bbo..yhadz.com

String.lastIndexOf ()方法返回字符串中子字符串最后一次出现的索引。

索引.js
const str = 'bobbyhadz.com'; const lastIndex = str.lastIndexOf('b'); console.log(lastIndex); // 👉️ 3

-1如果子字符串不包含在字符串中,则该方法返回。

我们使用该String.slice()方法获取要替换的字符前后的字符串部分。

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

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

姓名 描述
起始索引 要包含在返回的子字符串中的第一个字符的索引
结束索引 要从返回的子字符串中排除的第一个字符的索引
索引.js
const str = 'bobbyhadz.com'; const lastIndex = str.lastIndexOf('b'); console.log(lastIndex); // 👉️ 3 // 👇️ bob console.log(str.slice(0, lastIndex)); // 👇️ yhadz.com console.log(str.slice(lastIndex + 1));

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

我们1在第二次调用该方法时添加了起始索引str.slice(),以从结果中省略要替换的字符。

如果需要用多个字符替换最后一次出现的单个字符,也可以使用此方法。

索引.js
const str = 'bobbyhadz.com'; const lastIndex = str.lastIndexOf('b'); const replacement = '...'; const replaced = str.slice(0, lastIndex) + replacement + str.slice(lastIndex + 1); console.log(replaced); // 👉️ bob...yhadz.com

代码示例将最后一次出现的 替换b为多个字符。

如果您需要用多个字符替换最后一次出现的多个字符,请使用替换字符串的长度来确定第二次调用中的起始索引String.slice()

索引.js
const str = 'bbobbyhadz.com'; const lastIndex = str.lastIndexOf('bb'); const replacement = '..'; const replaced = str.slice(0, lastIndex) + replacement + str.slice(lastIndex + replacement.length); console.log(replaced); // 👉️ bbo..yhadz.com

我们实际上跳过了与替换字符串中一样多的字符。

如果您必须经常替换最后一次出现的字符,请创建一个可重用的函数。

索引.js
function replaceLastOccurrence(string, replacement) { const lastIndex = str.lastIndexOf('b'); const replaced = string.slice(0, lastIndex) + replacement + string.slice(lastIndex + replacement.length); return replaced; } const str = 'bobbyhadz.com'; // 👇️ bob.yhadz.com console.log(replaceLastOccurrence(str, '.'));

该函数将字符串和替换字符串作为参数,并替换字符串中最后一次出现的子字符串。

如果字符串不包含提供的字符,则indexOf方法返回。-1

如果您的应用程序中可能出现这种情况,请在if
语句中检查它以避免可能的错误。

索引.js
const str = 'Hello World'; // 👇️ 9 const lastIndex = str.lastIndexOf('l'); const replacement = '.'; let replaced; if (lastIndex !== -1) { replaced = str.slice(0, lastIndex) + replacement + str.slice(lastIndex + 1); } console.log(replaced); // 👉️ Hello Wor.d

在替换字符的最后一次出现之前,我们检查lastIndex变量是否不等于。-1

请注意,我们replaced使用let关键字声明了变量,因此我们可以在if语句中重新分配它。

我们希望replaced变量可以在if块外访问,所以我们在块外声明了它。

replaced如果对该
lastIndexOf()方法的调用没有返回,该变量将只存储一个字符串-1

或者,您可以使用该String.split()方法。

使用 split() 替换字符串中最后一次出现的字符

这是一个三步过程:

  1. 使用该lastIndexOf()方法获取字符的最后一个索引。
  2. 使用String.split()方法将字符串拆分为字符数组。
  3. 替换索引处的数组元素。
索引.js
function replaceLastOccurrence(string, replacement) { const lastIndex = str.lastIndexOf('b'); const arr = string.split(''); console.log(arr); arr[lastIndex] = replacement; return arr.join(''); } const str = 'bobbyhadz.com'; // 👇️ bob.yhadz.com console.log(replaceLastOccurrence(str, '.'));

我们使用该String.split()方法将字符串拆分为字符数组。

索引.js
const str = 'bobbyhadz.com'; // [ // 'b', 'o', 'b', 'b', // 'y', 'h', 'a', 'd', // 'z', '.', 'c', 'o', // 'm' // ] console.log(str.split(''));

我们使用该lastIndexOf()方法获取字符串中字符的最后一个索引,并
替换
该索引处的
数组元素。

最后一步是使用该Array.join()方法将数组连接成一个字符串。

索引.js
const str = 'bobbyhadz.com'; const arr = str.split(''); arr[3] = '.'; const result = arr.join(''); // 👇️ bob.yhadz.com console.log(result);

您还可以使用该String.substring()方法替换字符串中最后一次出现的字符。

使用 substring() 替换字符串中最后一次出现的字符

这是一个三步过程:

  1. 使用该lastIndexOf()方法获取字符的最后一个索引。
  2. 两次使用该substring()方法获取字符前后的部分。
  3. 在字符串的两部分之间添加替换字符串。
索引.js
const str = 'bobbyhadz.com'; const lastIndex = str.lastIndexOf('b'); const replacement = '.'; const replaced = str.substring(0, lastIndex) + replacement + str.substring(lastIndex + 1); console.log(replaced); // 👉️ bob.yhadz.com

String.substring
()
方法返回从开始索引到排除结束索引的字符串片段。

该方法采用以下参数:

姓名 描述
起始索引 要包含在返回的子字符串中的第一个字符的索引
结束索引 要从返回的子字符串中排除的第一个字符的索引

如果未end指定索引,则切片将转到字符串的末尾。

我们String.substring()以与使用该方法类似的方式使用该
String.slice()方法。

但是,和方法
之间
存在一些
差异String.substring()String.slice()

  • substring()如果开始索引大于结束索引,则该方法交换其开始索引和结束索引。在这种情况下,该slice()方法返回一个空字符串。
索引.js
const str = 'bobby'; console.log(str.substring(3, 0)); // 👉️ bob console.log(str.slice(3, 0)); // 👉️ ''
  • 如果传递给的两个参数中的任何一个都substring()为负数,则将它们视为0.
索引.js
const str = 'bobby'; console.log(str.substring(-3)); // 👉️ bobby console.log(str.slice(-3)); // 👉️ bby

当给定一个负索引时,该slice()方法从字符串的末尾向后计数以找到索引。

在 JS 中删除字符串中最后一次出现的字符

要从字符串中删除最后一次出现的字符:

  1. 使用该lastIndexOf()方法获取字符的最后一个索引。
  2. 两次使用该slice()方法获取字符前后的部分。
  3. 使用加法 (+) 运算符连接字符串的两个部分。
索引.js
function removeLastOccurrence(string, char) { const lastIndex = string.lastIndexOf(char); return string.slice(0, lastIndex) + str.slice(lastIndex + 1); } const str = 'hello world'; // 👇️ hello word console.log(removeLastOccurrence(str, 'l')); // 👇️ hello wrld console.log(removeLastOccurrence(str, 'o'));

String.lastIndexOf ()方法返回字符串中子字符串最后一次出现的索引。

-1如果子字符串不包含在字符串中,则该方法返回。

一旦我们有了字符串中字符的最后一个索引,我们就会得到包含字符串其余部分的 2 个子字符串,不包括特定索引处的字符。

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

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

姓名 描述
起始索引 要包含在返回的子字符串中的第一个字符的索引
结束索引 要从返回的子字符串中排除的第一个字符的索引

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

在我们第一次调用该slice()方法时,我们从 index 处的字符开始
0,直到但不包括该字符最后一次出现的索引。

索引.js
const str = 'hello world'; const lastIndex = str.lastIndexOf('l'); console.log(str.slice(0, lastIndex)); // 👉️ hello wor console.log(str.slice(lastIndex + 1)); // 👉️ d
JavaScript 索引是从零开始的,因此字符串中的第一个字符的索引为0,最后一个字符的索引为 string.length - 1

在我们对该slice()方法的第二次调用中,我们添加了1字符的最后一个索引,因为我们不希望它包含在新字符串中。

从字符串中删除最后一次出现的子串

这种方法可用于从字符串中删除最后一次出现的子字符串,它不必是单个字符。

索引.js
function removeLastOccurrence(string, substring) { const lastIndex = string.lastIndexOf(substring); return string.slice(0, lastIndex) + str.slice(lastIndex + substring.length); } const str = 'abc 123 abc 123'; // 👇️ 'abc 123 123' console.dir(removeLastOccurrence(str, 'abc'));

我们没有将最后一个索引加 1,而是使用子字符串的长度来确定从哪里开始提取。

需要注意的一个非常重要的事情是,
如果在字符串中找不到该字符,则该
lastIndexOf方法将返回。-1

要处理这种情况,您可以使用一条if语句:

索引.js
function removeLastOccurrence(string, substring) { const lastIndex = string.lastIndexOf(substring); if (lastIndex !== -1) { return string.slice(0, lastIndex) + str.slice(lastIndex + substring.length); } return string; } const str = 'abc 123 abc 123'; // 👇️ 'abc 123 123' console.dir(removeLastOccurrence(str, 'abc')); // 👇️ 'abc 123 abc 123' console.dir(removeLastOccurrence(str, 'XYZ'));

在从字符串中删除最后一次出现的子字符串之前,我们使用了一条if语句来检查该lastIndexOf方法是否没有返回
-1

如果方法返回-1,则子字符串不包含在字符串中,因此我们按原样返回字符串。

额外资源

您可以通过查看以下教程来了解有关相关主题的更多信息: