Yes, on principle, these 3 members behave in the same manner. However, their subtle differences you need to consider.
slice(startIndex[, endIndex]):It returns the string between beginIndex and endIndex. Here it will return as a new string.
// Syntax: string.slice(startIndex [, endIndex]) var string1 = "How cool and delicious it was!"; console.log(string1.slice(4,9)) // Output: 'cool'
substr(start[, length]): It returns the string starting from start index until the number of character specified in length parameter.
// Syntax: string.substr(start [, length]) var string2 = "The water was cool and had a metallic taste."; console.log(string2.substr(3,7)); // Output: 'water'
substring(indexStart[, indexEnd]): It returns the string between the indexStart and indexEnd. Alternatively, it returns the end of the string.
// syntax: string.substring(start [, stop]) var string3 = "Nowadays, the social reformer is cool and hip!"; console.log(string3.substring(4,8)); // Output: 'days'
What if you pass an invalid value or negative numbers as parameters? rules are in place to deal with such scenarios. What if you pass a negative number via slice? In this case, it subtracts the number from the length of the string.
Negative number examples for string methods
var string = "Hello this is test"; string.substring(-4); // "Hello this is test" string.substr(-4); // Output: "st" string.slice(-4); // Output: "test"
Now let’s see the case of substr. When we pass a negative number, it will be converted to zero. In case the length or start parameters have NaN, then it will be converted to zero. In case you pass a negative number as a start, an index will be set to the string length minus value.
When it comes to a lot of IT systems, flat data files are standard. When we speak of file formats, we consider them the lowest common denominator. You will come across plenty of field delineators like commas or tabs. In some cases, fixed width rows are present too in text files.
As the consumer, you have got a clear idea about the number of characters present in each field. Let’s assume that a row is supposed to have 300 characters whereas a filed named ‘name’ takes the first forty characters. When you don’t use a character, you will pad it with space.
To extract the name field, you may use any of the three standard methods.
// Examples dataRecord.substring(0,40); dataRecord.slice(0, 40); dataRecord.substr(0,40);
In the example that I have used, I just took a sentence and inserted the name “Tony Stark” at the very start. Then I added the remaining characters with spaces. To trace it without any difficulty, I chose ‘N’ as the last character. We hope that you have got a fair idea as for how we extract a string with all 3 methods.
// Examples var dataRecord = "Tony Stark N" dataRecord.substring(0, 40); // Output: "Tony Stark N" dataRecord.slice(0, 40); // Output: "Tony Stark N" dataRecord.substr(0,40); // Output: "Tony Stark N"
Well, now let’s examine the next field named, place. We are going to find it interesting. The place field can accommodate 30 characters. If we are to use the same methods with the same parameters, we will get the following output:
// Examples var dataRecord = "Tony Start New York H" dataRecord.substring(30, 20); // Output: " " dataRecord.substr(30, 20); // Output: "New York H" dataRecord.slice(30, 20); // "" dataRecord.substring(30, 50); // Output: "New York H" dataRecord.slice(30, 50); // Output: "New York H"
Well, we plan to pass values 30 and 20 for start index and field length respectively. As you can see in the above example, the only substr seems to work in this case.
Now we will see the case of the slice. Again it’s different. Given 30>20, it is going to return an empty string as a substring. From what we have learnt about the slice, it is meant to return to an empty string when we pass a value like this.
Yes, the key takeaway is that slice and substring are similar. The significant difference is that the slice can accept a negative number.
In the case of a slice, negative numbers will be converted to zero in the substring.
As for substr, it is again index based when it comes to the starting position. The length of the characters to extract is what we pass as the second parameter value. When we pass a negative length, it will start converting characters from the end of the string.
In conclusion, all 3 methods are helpful if you wish to extract a portion of a string. However, you need to know the positions of the value.