![]() If startIndex > endIndex, then the substring method swaps the arguments and returns a substring, assuming as the endIndex > startIndex.If startIndex and endIndex are both greater than the length of the string, it returns an empty string.If startIndex = endIndex, the substring method returns an empty string.Then we get a substring starting from the 6th character until the end of the original string. However, if we set only a starting index and no ending index for this example: ![]() 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: The first character's index is always 0 Suppose that we have the example string below: const myString = "I am learning JavaScript and it is cool!" endIndex: represents the ending point of the substring (optional).startIndex: represents the starting point of the substring.The substring method expects two parameters: string.substring(startIndex, endIndex) ![]() This method basically gets a part of the original string and returns it as a new string. Let’s start with the substring( ) method. You can also watch the video version of the example usages here: 1. Now let’s see how to do that in JavaScript in 3 different ways. Like in the example above, in some cases we need to get one or more substrings from a complete sentence or a paragraph. "JavaScript is cool!" -> Another Substring What is a Substring?Ī substring is a subset of another string: "I am learning JavaScript and it is cool!" -> Original String But first, let me explain briefly what a substring is. ![]() In this article, you’re going to learn how to get a substring by using 3 different built-in methods. Getting a substring from a string is one of the most common operations in JavaScript. We can use these methods for various operations like searching, replacing, concatenating strings, and so on. Fortunately, there are many built-in methods in JavaScript that help us while working with arrays, strings and other data types. In daily programming, we often need to work with strings. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |