If I have this:
console.log('12345'.slice(-3)); // I get '345'
but when I do:
console.log('12345'.slice(-3, 0)); // I get '' (empty string).
what is the reasoning behind this? It makes no sense to me. Is there a JS API that can wrap around strings like I expect here? (I was expecting '345' again.)
The second parameter is the index where the substring to be extracted ends (exclusive). If that index is before (or equal to) the start index (the first parameter), the result is the empty string.
As the docs say about the second parameter:
If end is positioned before or at start after normalization, nothing is extracted.
After normalization, .slice(-3) is equivalent to .slice(2), and the second parameter defaults to the length of the string, so
.slice(-3)
// equivalent to
.slice(2)
is like
.slice(2, 5) // take the substring from index 2 to index 5
and not
.slice(2, 0)
I have an array - let's call it array.
When it has one element e.g. [1], array.slice(-1) returns [1].
However, array.slice(0,-1) returns [].
Why is this difference?
From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
Case 1: negative index as single parameter (start):
"A negative index can be used, indicating an offset from the end of the sequence. slice(-1) extracts the last element in the sequence."
Case 2: negative index as second parameter (end):
"The index of the first element to exclude from the returned array."
You can play around with different parameters here.
I'm trying to understand a lot of the basic components of Javascript and one of the things I came across is a line of code saying
if (varX.indexOf(String(varY),0) < 0)
varX being an array of Strings and varY being obviously one of the strings within that array. Take away the ",0" and I understand that the code is just looking for varY withing array varX. But I don't know what the ,0 does and what means for the if statement. I did what I could to look this up and didn't really come across anything.
According to the MDN docs:
fromindex
The index to start the search at. If the index is greater than or equal to the array's length, -1 is returned, which means the array will not be searched. If the provided index value is a negative number, it is taken as the offset from the end of the array. Note: if the provided index is negative, the array is still searched from front to back. If the calculated index is less than 0, then the whole array will be searched. Default: 0 (Entire array is searched).
So, passing in "0" is pretty much pointless, as it starts off the search at 0 anyway.
From mdn:
arr.indexOf(searchElement[, fromIndex = 0])
fromIndex
The index to start the search at. If the index is greater than or
equal to the array's length, -1 is returned, which means the array
will not be searched. If the provided index value is a negative
number, it is taken as the offset from the end of the array. Note: if
the provided index is negative, the array is still searched from front
to back. If the calculated index is less than 0, then the whole array
will be searched. Default: 0 (Entire array is searched).
0 is index from where you start the search. It is 0 by default, so you don't have to pass this parameter.
Recently I was asked in an interview, where the interviewer asked me this above question.
I was surely perplexed and answered him by using a for loop implementation where
we would find the length of the given "text" (using JavaScript) ... str.length() ...
we would take the first element of the "pattern" and find that in the "text".
If found we would increment the array .. As strings are stored as array in JavaScript ..
And we would find the "(a substring)" in similar way..
know this might be wrong way , but can anyone suggest a better way ? Thank you :-)
String.prototype.search (regexp)
When the search method is called with argument regexp, the following steps are taken:
Call CheckObjectCoercible passing the this value as its argument.
Let string be the result of calling ToString, giving it the this value as its argument.
If Type(regexp) is Object and the value of the [[Class]] internal property of regexp is "RegExp", then let rx be regexp;
Else, let rx be a new RegExp object created as if by the expression new RegExp(regexp) where RegExp is the standard built-in constructor with that name.
Search the value string from its beginning for an occurrence of the regular expression pattern rx. Let result be a Number indicating the offset within string where the pattern matched, or –1 if there was no match. The lastIndex and global properties of regexp are ignored when performing the search. The lastIndex property of regexp is left unchanged.
Return result.
Or if you want to avoid the word RegExp alltogether and search for a sub-string then
String.prototype.indexOf (searchString, position)
If searchString appears as a substring of the result of converting this object to a String, at one or more positions that are greater than or equal to position, then the index of the smallest such position is returned; otherwise, ‑1 is returned. If position is undefined, 0 is assumed, so as to search all of the String.
The indexOf method takes two arguments, searchString and position, and performs the following steps:
Call CheckObjectCoercible passing the this value as its argument.
Let S be the result of calling ToString, giving it the this value as its argument.
Let searchStr be ToString(searchString).
Let pos be ToInteger(position). (If position is undefined, this step produces the value 0).
Let len be the number of characters in S.
Let start be min(max(pos, 0), len).
Let searchLen be the number of characters in searchStr.
Return the smallest possible integer k not smaller than start such that k+ searchLen is not greater than len, and for all nonnegative integers j less than searchLen, the character at position k+j of S is the same as the character at position j of searchStr; but if there is no such integer k, then return the value -1.
I think the answer depends on the context, and the context seems to be lacking.
If the question was about algorithms, then I believe your best choice of algorithms (i.e., the fastest algorithm) is Boyer–Moore string search.
If the question was a HowTo in PHP, then its probably string.indexOf. And str.search would probably not be a valid answer since it takes a regex.
S[i] refers to the character at index i of string S, counting from 1.
S[i..j] refers to the substring of string S starting at index i and ending at j, inclusive.
A prefix of S is a substring S[1..i] for some i in range [1, n], where n is the length of S.
A suffix of S is a substring S[i..n] for some i in range [1, n], where n is the length of S.
The string to be searched for is called the pattern and is referred to with symbol P.
The string being searched in is called the text and is referred to with symbol T.
The length of P is n.
The length of T is m.
An alignment of P to T is an index k in T such that the last character of P is aligned with index k of T.
A match or occurrence of P occurs at an alignment if P is equivalent to T[(k-n+1)..k].
Does anyone know what the difference is between these two methods?
String.prototype.slice
String.prototype.substring
slice() works like substring() with a few different behaviors.
Syntax: string.slice(start, stop);
Syntax: string.substring(start, stop);
What they have in common:
If start equals stop: returns an empty string
If stop is omitted: extracts characters to the end of the string
If either argument is greater than the string's length, the string's length will be used instead.
Distinctions of substring():
If start > stop, then substring will swap those 2 arguments.
If either argument is negative or is NaN, it is treated as if it were 0.
Distinctions of slice():
If start > stop, slice() will return the empty string. ("")
If start is negative: sets char from the end of string, exactly like substr() in Firefox. This behavior is observed in both Firefox and IE.
If stop is negative: sets stop to: string.length – Math.abs(stop) (original value), except bounded at 0 (thus, Math.max(0, string.length + stop)) as covered in the ECMA specification.
Source: Rudimentary Art of Programming & Development: Javascript: substr() v.s. substring()
TL;DR:
If you know the index (the position) on which you'll stop (but NOT include), use slice().
If you know the length of characters to be extracted, you could use substr(), but that is discouraged as it is deprecated.
Otherwise, read on for a full comparison
Syntax
string.slice(start,end)
string.substr(start,length)
string.substring(start,end)
Note #1: slice()==substring()
What it does?
slice() extracts parts of a string and returns the extracted parts in a new string.
substr() extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters.
substring() extracts parts of a string and returns the extracted parts in a new string.
Note #2: slice()==substring()
Changes the Original String?
slice() doesn't
substr() doesn't
substring() doesn't
Note #3: slice()==substr()==substring()
Using Negative Numbers as an Argument
slice() selects characters starting from the end of the string
substr() selects characters starting from the end of the string
substring() doesn't perform
Note #4: slice()==substr()
If the First Argument is Greater than the Second
slice() doesn't perform
substr() since the Second Argument is NOT a position, but length value, it will perform as usual, with no problems
substring() will swap the two arguments, and perform as usual
The First Argument
slice() required; starting Index
substr() required; starting Index
substring() required; starting Index
Note #5: slice()==substr()==substring()
The Second Argument
slice() optional; the position (up to, but not including) where to end the extraction
substr() optional; the number of characters to extract
substring() optional; the position (up to, but not including) where to end the extraction
Note #6: slice()==substring()
What if the Second Argument is Omitted?
slice() selects all characters from the start-position to the end of the string
substr() selects all characters from the start-position to the end of the string
substring() selects all characters from the start-position to the end of the string
Note #7: slice()==substr()==substring()
So, you can say that there's a difference between slice() and substr(), while substring() is basically a copy of slice().
If you want substr's functionality:
"foobarbaz".substr(index, length);
without using a deprecated feature, you can just do:
"foobarbaz".substring(index, length + index);
And get the exact same results bar all of the edge-cases, like negative index/length.
Ben Nadel has written a good article about this, he points out the difference in the parameters to these functions:
String.slice( begin [, end ] )
String.substring( from [, to ] )
String.substr( start [, length ] )
He also points out that if the parameters to slice are negative, they reference the string from the end. Substring and substr doesn't.
Here is his article about this.
The one answer is fine but requires a little reading into. Especially with the new terminology "stop".
My Go -- organized by differences to make it useful in addition to the first answer by Daniel above:
1) negative indexes. Substring requires positive indexes and will set a negative index to 0. Slice's negative index means the position from the end of the string.
"1234".substring(-2, -1) == "1234".substring(0,0) == ""
"1234".slice(-2, -1) == "1234".slice(2, 3) == "3"
2) Swapping of indexes. Substring will reorder the indexes to make the first index less than or equal to the second index.
"1234".substring(3,2) == "1234".substring(2,3) == "3"
"1234".slice(3,2) == ""
--------------------------
General comment -- I find it weird that the second index is the position after the last character of the slice or substring. I would expect "1234".slice(2,2) to return "3". This makes Andy's confusion above justified -- I would expect "1234".slice(2, -1) to return "34". Yes, this means I'm new to Javascript. This means also this behavior:
"1234".slice(-2, -2) == "", "1234".slice(-2, -1) == "3", "1234".slice(-2, -0) == "" <-- you have to use length or omit the argument to get the 4.
"1234".slice(3, -2) == "", "1234".slice(3, -1) == "", "1234".slice(3, -0) == "" <-- same issue, but seems weirder.
My 2c.
The difference between substring and slice - is how they work with negative and overlooking lines abroad arguments:
substring(start, end)
Negative arguments are interpreted as zero. Too large values are truncated to the length of the string:
alert("testme".substring(-2)); // "testme", -2 becomes 0
Furthermore, if start > end, the arguments are interchanged, i.e. plot line returns between the start and end:
alert("testme".substring(4, -1)); // "test"
// -1 Becomes 0 -> got substring (4, 0)
// 4> 0, so that the arguments are swapped -> substring (0, 4) = "test"
slice
Negative values are measured from the end of the line:
alert("testme".slice(-2)); // "me", from the end position 2
alert("testme".slice(1, -1)); // "estm", from the first position to the one at the end.
It is much more convenient than the strange logic substring.
A negative value of the first parameter to substr supported in all browsers except IE8-.
If the choice of one of these three methods, for use in most situations - it will be slice: negative arguments and it maintains and operates most obvious.
substr: It's providing us to fetch part of the string based on specified index.
syntax of substr-
string.substr(start,end)
start - start index tells where the fetching start.
end - end index tells upto where string fetches. It's optional.
slice: It's providing to fetch part of the string based on the specified index. It's allows us to specify positive and index.
syntax of slice - string.slice(start,end)
start - start index tells where the fetching start.It's
end - end index tells upto where string fetches. It's optional.
In 'splice' both start and end index helps to take positive and negative index.
sample code for 'slice' in string
var str="Javascript";
console.log(str.slice(-5,-1));
output: crip
sample code for 'substring' in string
var str="Javascript";
console.log(str.substring(1,5));
output: avas
[*Note: negative indexing starts at the end of the string.]
The only difference between slice and substring method is of arguments
Both take two arguments e.g. start/from and end/to.
You cannot pass a negative value as first argument for substring method but for slice method to traverse it from end.
Slice method argument details:
Arguments
start_index
Index from where slice should begin. If value is provided in negative it means start from last. e.g. -1 for last character.
end_index
Index after end of slice. If not provided slice will be taken from start_index to end of string. In case of negative value index will be measured from end of string.
Substring method argument details:
Arguments
from
It should be a non negative integer to specify index from where sub-string should start.
to
An optional non negative integer to provide index before which sub-string should be finished.
For slice(start, stop), if stop is negative, stop will be set to:
string.length – Math.abs(stop)
rather than:
string.length – 1 – Math.abs(stop)