3种不同的内置方法来获取子字符串的操作用法

  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中有许多内置方法可以在处理数组js 字符串 substr,字符串和其他数据类型时为我们提供帮助。 我们可以将这些方法用于各种操作js 字符串 substr,例如搜索,替换,串联字符串等。

  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:

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

  <pre class="has">`"I am learning JavaScript and it is cool!" --> Original String
"I am learning JavaScript" --> Substring
"JavaScript is cool!" --> Another Substring`</pre>

  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中进行操作。

  js substring和substr_js 字符串 substr_js str substr

  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方法需要两个参数:

  <pre class="has">string.substring(startIndex, endIndex);</pre>

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

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

  <pre class="has">const myString = "I am learning JavaScript and it is cool!";</pre>

  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:

  其他一些要点:

  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方法还需要相同的两个参数:

  <pre class="has">string.slice(startIndex, endIndex);</pre>

  substring()和slice()方法的共同点: (The common points of substring( ) and slice( ) methods:) slice()方法的区别: (Differences of the slice( ) method:)

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

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

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

  , 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.

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

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

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

  <pre class="has">string.substring(startIndex, length);</pre>

  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):

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

  js str substr_js substring和substr_js 字符串 substr

  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!

  感谢您的阅读!

  翻译自:

  js字符串slice

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1866
0 评论
367

发表评论

!