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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390523.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

leetcode 218. 天际线问题

城市的天际线是从远处观看该城市中所有建筑物形成的轮廓的外部轮廓。给你所有建筑物的位置和高度,请返回由这些建筑物形成的 天际线 。 每个建筑物的几何信息由数组 buildings 表示,其中三元组 buildings[i] [lefti, righti, heighti] 表示&#xff1a…

[Android Pro] 终极组件化框架项目方案详解

cp from : https://blog.csdn.net/pochenpiji159/article/details/78660844 前言 本文所讲的组件化案例是基于自己开源的组件化框架项目github上地址github.com/HelloChenJi…其中即时通讯(Chat)模块是单独的项目github上地址github.com/HelloChenJi… 1.什么是组件化&#xff…

如何写一个vue指令directive

举个例子 :clickoutside.js const clickoutsideContext clickoutsideContext;export default {/*param el 指令所绑定的元素param binding {Object} param vnode vue编译生成的虚拟节点*/bind (el, binding, vnode) {const documentHandler function(e) {console.…

安装angular cli_Angular 9适用于初学者—如何使用Angular CLI安装第一个应用程序

安装angular cliAngular is one of the most popular JavaScript frameworks created and developed by Google. In the last couple of years, ReactJS has gained a lot of interest and has become the most popular modern JS library in the industry. But this doesn’t …

leetcode 1818. 绝对差值和

给你两个正整数数组 nums1 和 nums2 &#xff0c;数组的长度都是 n 。 数组 nums1 和 nums2 的 绝对差值和 定义为所有 |nums1[i] - nums2[i]|&#xff08;0 < i < n&#xff09;的 总和&#xff08;下标从 0 开始&#xff09;。 你可以选用 nums1 中的 任意一个 元素来…

【转载】keil5中加入STM32F10X_HD,USE_STDPERIPH_DRIVER的原因

初学STM32&#xff0c;在RealView MDK 环境中使用STM32固件库建立工程时&#xff0c;初学者可能会遇到编译不通过的问题。出现如下警告或错误提示&#xff1a; warning: #223-D: function "assert_param" declared implicitly;assert_param(IS_GPIO_ALL_PERIPH(GPIOx…

下岗职工_下岗后我如何获得多位软件工程师的面试

下岗职工“Opportunities to find our deeper powers come when life seems most challenging.” -Joseph Campbell“当生活似乎最具挑战性时&#xff0c;就有机会找到我们更深层的力量。” 约瑟夫坎贝尔 I was recently laid off for the first time in my life. I realized t…

1846. 减小和重新排列数组后的最大元素

给你一个正整数数组 arr 。请你对 arr 执行一些操作&#xff08;也可以不进行任何操作&#xff09;&#xff0c;使得数组满足以下条件&#xff1a; arr 中 第一个 元素必须为 1 。任意相邻两个元素的差的绝对值 小于等于 1 &#xff0c;也就是说&#xff0c;对于任意的 1 <…

bashdb常用命令

一、列出代码和查询代码类&#xff1a; l 列出当前行以下的10行- 列出正在执行的代码行的前面10行. 回到正在执行的代码行w 列出正在执行的代码行前后的代码/pat/ 向后搜索pat&#xff1f;pat&#xff1f;向前搜索pat二、Debug控制类&#xff1a; h 帮助help 命令 得到…

podcast播客资源_为什么播客是我的新维基百科-完美的非正式学习资源

podcast播客资源In this article, I’ll explain why podcasts replaced a lot of my Wikipedia usage for informal learning. I’ll also talk about how I listen to 5 hours of podcasts every day.在本文中&#xff0c;我将解释为什么播客代替了我的许多Wikipedia用于非正…

剑指 Offer 53 - I. 在排序数组中查找数字 I(二分法)

统计一个数字在排序数组中出现的次数。 示例 1: 输入: nums [5,7,7,8,8,10], target 8 输出: 2 示例 2: 输入: nums [5,7,7,8,8,10], target 6 输出: 0 限制&#xff1a; 0 < 数组长度 < 50000 解题思路 先用二分法查找出其中一个目标元素再向目标元素两边查找…

MVC与三层架构区别

我们平时总是将三层架构与MVC混为一谈&#xff0c;殊不知它俩并不是一个概念。下面我来为大家揭晓我所知道的一些真相。 首先&#xff0c;它俩根本不是一个概念。 三层架构是一个分层式的软件体系架构设计&#xff0c;它可适用于任何一个项目。 MVC是一个设计模式&#xff0c;它…

tensorflow 实现逻辑回归——原以为TensorFlow不擅长做线性回归或者逻辑回归,原来是这么简单哇!...

实现的是预测 低 出生 体重 的 概率。尼克麦克卢尔&#xff08;Nick McClure&#xff09;. TensorFlow机器学习实战指南 (智能系统与技术丛书) (Kindle 位置 1060-1061). Kindle 版本. # Logistic Regression #---------------------------------- # # This function shows ho…

sdlc 瀑布式 生命周期_SDLC指南–软件开发生命周期的阶段和方法

sdlc 瀑布式 生命周期When I decided to teach myself how to code almost four years ago I had never heard of, let alone thought about, the software development life cycle.当我差不多四年前决定教自己如何编码时&#xff0c;我从未听说过软件开发生命周期&#xff0c;…

剑指 Offer 48. 最长不含重复字符的子字符串

请从字符串中找出一个最长的不包含重复字符的子字符串&#xff0c;计算该最长子字符串的长度。 示例 1: 输入: “abcabcbb” 输出: 3 解释: 因为无重复字符的最长子串是 “abc”&#xff0c;所以其长度为 3。 示例 2: 输入: “bbbbb” 输出: 1 解释: 因为无重复字符的最长子…

Mysql-my-innodb-heavy-4G.cnf配置文件注解

Mysql-同Nginx等一样具备多实例的特点&#xff0c;简单的讲就是在一台服务器上同时开启多个不同的服务端口&#xff08;3306,3307&#xff09;同时运行多个Mysql服务进程&#xff0c;这些服务进程通过不同的socket监听不同的服务端口来提供服务。这些Mysql多实例公用一套Mysql安…

is 和 == 的区别

is 和 操作符的区别 python官方解释&#xff1a; 的meaning为equal&#xff1b; is的meaning为object identity&#xff1b; is 判断对象是否相等&#xff0c;即身份是否相同&#xff0c;使用id值判断&#xff1b; 判断对象的值是否相等。id值是什么&#xff1f;id()函数官网…

win10管理凌乱桌面_用于管理凌乱的开源存储库的命令行技巧

win10管理凌乱桌面Effective collaboration, especially in open source software development, starts with effective organization. To make sure that nothing gets missed, the general rule, “one issue, one pull request” is a nice rule of thumb.有效的协作(特别是…

JAVA数组Java StringBuffer 和 StringBuilder 类

版权声明&#xff1a;本文为博主原创文章&#xff0c;未经博主允许不得转载。 https://blog.csdn.net/qq_34173549/article/details/80215173 Java StringBuffer 和 StringBuilder 类 当对字符串进行修改的时候&#xff0c;需要使用 StringBuffer 和 StringBuilder 类。 和 Str…

strlen和sizeof的长度区别

strlen返回字符长度 而sizeof返回整个数组占多长&#xff0c;字符串的\0也会计入一个长度转载于:https://www.cnblogs.com/DawaTech/p/8086055.html