js字符串slice

In daily programming, we often need to work with strings. Fortunately, there are many built-in methods in JavaScript that help us while working with arrays, strings and other data types. We can use these methods for various operations like searching, replacing, concatenating strings, and so on.

在日常编程中,我们经常需要使用字符串。 幸运的是,JavaScript中有许多内置方法可以在处理数组,字符串和其他数据类型时为我们提供帮助。 我们可以将这些方法用于各种操作,例如搜索,替换,串联字符串等。

Getting a substring from a string is one of the most common operations in JavaScript. In this article, you’re going to learn how to get a substring by using 3 different built-in methods. But first, let me explain briefly what a substring is.

从字符串中获取子字符串是JavaScript中最常见的操作之一。 在本文中,您将学习如何使用3种不同的内置方法来获取子字符串。 但首先,让我简要解释一下什么是子字符串。

什么是子串? (What is a Substring?)

A substring is a subset of another string:

子字符串是另一个字符串的子集:

"I am learning JavaScript and it is cool!"  -->  Original String"I am learning JavaScript"  -->  Substring"JavaScript is cool!"  -->  Another Substring

Like in the example above, in some cases we need to get one or more substrings from a complete sentence or a paragraph. Now let’s see how to do that in JavaScript in 3 different ways.

像上面的示例一样,在某些情况下,我们需要从完整的句子或段落中获取一个或多个子字符串。 现在,让我们看看如何以3种不同的方式在JavaScript中进行操作。

You can also watch the video version of the example usages here:

您还可以在此处观看示例用法的视频版本:

1. substring()方法 (1. The substring( ) Method)

Let’s start with the substring( ) method. This method basically gets a part of the original string and returns it as a new string. The substring method expects two parameters:

让我们从substring()方法开始。 此方法基本上获取原始字符串的一部分,并将其作为新字符串返回。 substring方法需要两个参数:

string.substring(startIndex, endIndex);
  • startIndex: represents the starting point of the substring

    startIndex :代表子字符串的起点

  • endIndex: represents the ending point of the substring (optional)

    endIndex :代表子字符串的终点(可选)

Let’s see the usage in an example. Suppose that we have the example string below:

让我们在示例中查看用法。 假设下面有示例字符串:

const myString = "I am learning JavaScript and it is cool!";

Now if we set the startIndex as 0 and the endIndex as 10, then we will get the first 10 characters of the original string:

现在,如果将startIndex设置为0,将endIndex设置为10,则将获得原始字符串的前10个字符:

However, if we set only a starting index and no ending index for this example:

但是,如果在此示例中我们仅设置开始索引而没有设置结束索引:

Then we get a substring starting from the 6th character until the end of the original string.

然后我们得到一个从第6个字符开始直到原始字符串结尾的子字符串。

Some additional points:

其他一些要点:

  • If startIndex = endIndex, the substring method returns an empty string如果startIndex = endIndex,则substring方法将返回一个空字符串
  • If startIndex and endIndex are both greater than the length of the string, it returns an empty string如果startIndex和endIndex都大于字符串的长度,则返回一个空字符串
  • If startIndex > endIndex, then the substring method swaps the arguments and returns a substring, assuming as the endIndex > startIndex如果startIndex> endIndex,则子字符串方法交换参数并返回一个子字符串,假定为endIndex> startIndex

2. slice()方法 (2. The slice( ) Method)

The slice( ) method is similar to the substring( ) method and it also returns a substring of the original string. The slice method also expects the same two parameters:

slice()方法类似于substring()方法,并且它还返回原始字符串的子字符串。 slice方法还需要相同的两个参数:

string.slice(startIndex, endIndex);
  • startIndex: represents the starting point of the substring

    startIndex :代表子字符串的起点

  • endIndex: represents the ending point of the substring (optional)

    endIndex :代表子字符串的终点(可选)

substring()和slice()方法的共同点: (The common points of substring( ) and slice( ) methods:)

  • If we don’t set an ending index, then we get a substring starting from the given index number until the end of the original string:如果我们没有设置结束索引,那么我们将从给定的索引号开始直到原始字符串的末尾得到一个子字符串:
  • If we set both the startIndex and the endIndex, then we will get the characters between the given index numbers of the original string:如果我们同时设置了startIndex和endIndex,则将获得原始字符串的给定索引号之间的字符:
  • If startIndex and endIndex are greater than the length of the string, it returns an empty string如果startIndex和endIndex大于字符串的长度,则返回一个空字符串

slice()方法的区别: (Differences of the slice( ) method:)

  • If startIndex > endIndex, the slice( ) method returns an empty string如果startIndex> endIndex,则slice()方法返回一个空字符串
  • If startIndex is a negative number, then the first character begins from the end of the string (reverse):如果startIndex为负数,则第一个字符从字符串的末尾开始(反向):

Note: We can use the slice( ) method also for JavaScript arrays. You can find here my other article about the slice method to see the usage for arrays.

注意:我们也可以将slice()方法用于JavaScript数组。 您可以在此处找到有关slice方法的其他文章 ,以了解数组的用法。

3. substr()方法 (3. The substr( ) Method)

According to the Mozilla documents, the substr( ) method is considered a legacy function and its use should be avoided. But I will still briefly explain what it does because you might see it in older projects.

根据Mozilla文档 ,substr()方法被视为旧版函数,应避免使用它。 但是我仍然会简要解释它的作用,因为您可能会在较早的项目中看到它。

The substr( ) method also returns a substring of the original string and expects two parameters as:

substr()方法还返回原始字符串的子字符串,并期望两个参数为:

string.substring(startIndex, length);
  • startIndex: represents the starting point of the substring

    startIndex :代表子字符串的起点

  • length: number of characters to be included (optional)

    length :要包含的字符数(可选)

You can see the difference here: the substr( ) method expects the second parameter as a length instead of an endIndex:

您可以在此处看到区别:substr()方法将第二个参数作为长度而不是endIndex:

In this example, it basically counts 5 characters starting with the given startIndex and returns them as a substring.

在此示例中,它基本上从给定的startIndex开始计数5个字符,并将它们作为子字符串返回。

However, if we don’t define the second parameter, it returns up to the end of the original string (like the previous two methods do):

但是,如果我们不定义第二个参数,它将返回到原始字符串的末尾(就像前两个方法一样):

Note: All 3 methods return the substring as a new string and they don’t change the original string.

注意:所有3种方法都将子字符串作为新字符串返回,并且它们不会更改原始字符串。

结语 (Wrap up)

So these are the 3 different methods to get a substring in JavaScript. There are many other built-in methods in JS which really help us a lot when dealing with various things in programming. If you find this post helpful, please share it on social media.

因此,这是在JavaScript中获取子字符串的3种不同方法。 JS中还有许多其他内置方法,在处理编程中的各种问题时,它们确实对我们有很大帮助。 如果您发现此帖子有帮助,请在社交媒体上分享。

If you want to learn more about web development, feel free to follow me on Youtube!

如果您想了解有关Web开发的更多信息,请随时 在YouTube上关注我

Thank you for reading!

感谢您的阅读!

翻译自: https://www.freecodecamp.org/news/javascript-substring-examples-slice-substr-and-substring-methods-in-js/

js字符串slice

js字符串slice_JavaScript子字符串示例-JS中的Slice,Substr和Substring方法相关推荐

  1. 如何在Python中对字符串进行子字符串化

    Python offers many ways to substring a string. It is often called 'slicing'. Python提供了许多对字符串进行子字符串化的 ...

  2. Python判断字符串包含子字符串(个数、索引、全部位置)

    从左向右查找子串,存在则输出子串首字符的索引值,不存在则输出-1 # find()a = 'love you' b = 'you' c = 'no' print(a.find(b)) #5 print ...

  3. C语言检查一个字符串是否为另一个字符串的子字符串的算法(附完整源码)

    C语言检查一个字符串是否为另一个字符串的子字符串的算法 C语言检查一个字符串是否为另一个字符串的子字符串的算法完整源码(定义,实现,main函数测试) C语言检查一个字符串是否为另一个字符串的子字符串 ...

  4. 字符串和数组中split().toString(),join(),splice(),slice(),substr()和substring()

    1 <!Doctype html> 2 <head> 3 <mate charset="utf-8"> 4 <title>strin ...

  5. JS之检索子字符串的search方法

    作用:search() 方法用于检索字符串中指定的子字符串,或检索与正则表达式相匹配的子字符串 语法:stringObject.search(regexp) 参数:该参数可以是需要在 stringOb ...

  6. 如何在Python中获取字符串的子字符串?

    有没有一种方法可以在Python中为字符串加上字符串,以从第三个字符到字符串的末尾获取新的字符串? 也许像myString[2:end] ? 如果离开第二部分意味着"直到最后",而 ...

  7. JS截取字符串substr 和 substring方法的区别

    substr 方法 返回一个从指定位置开始的指定长度的子字符串.stringvar.substr(start [, length ])参数 stringvar必选项.要提取子字符串的字符串文字或 St ...

  8. Python是否具有字符串“包含”子字符串方法?

    我正在寻找Python中的string.contains或string.indexof方法. 我想要做: if not somestring.contains("blah"):co ...

  9. 字符串之子字符串查找

    子字符串查找   字符串的一种基本操作就是子字符串查找:给定一段长度为N的文本text和一个长度为M的模式字符串pattern,在文本中找到一个和该模式相符的子字符串.解决该问题的大部分算法都可以很容 ...

最新文章

  1. 云原生安全的挑战与实践
  2. 【高并发】高并发环境下诡异的加锁问题(你加的锁未必安全)
  3. POJ 3613 快速幂+Floyd变形(求限制k条路径的最短路)
  4. float 精度_float相加产生精度损失的原因是什么?
  5. Google Chrome —— 使用 PowerShell 命令安装 Google Chrome 浏览器
  6. php接收arduino向服务器发来的请求
  7. vb中filecopy拷贝文件
  8. 你要了解的11款面向Linux系统的一流备份实用工具
  9. linux command read the content,Linux while 和 read 的用法
  10. (转)android之Fragment(官网资料翻译)
  11. 高数叔c语言课件,高数叔网课资源合集 高数上下、线代、模电、物理、复变合集...
  12. 【QT C++ TCP通信调试助手】
  13. php匹配正则的方法,PHP实现正则匹配操作的方法
  14. 高通WLAN框架学习(3)- -WLAN FTM 模式
  15. 程序员坐行李箱迎寒风编码 2 小时,目击者:激励我写了一篇论文!
  16. firewall和企业级的Open ViP服务
  17. lsa ospf的opaque_OSPF LSA类型详解
  18. 编译原理及编译程序构造-绪论
  19. 现成饮料的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  20. 【图像超分辨率】(VDSR)Accurate Image Super-Resolution Using Very Deep Convolutional Networks

热门文章

  1. 06-图1 列出连通集 (25 分)
  2. java接口的定义与实现,学习路线+知识点梳理
  3. win10计算机磁盘图标,Win10 21H1怎么更换电脑磁盘的图标标识
  4. Java 集合 之 Vector
  5. 不同账号间的云资源授权方法
  6. RUNOOB python练习题31 根据已输入的字符判断星期几
  7. Delphi XE2获取汉字拼音首字母
  8. Python高级网络编程系列之基础篇
  9. redis memcached MongoDB
  10. shell 字符串分割