quote:
Originally posted by Huhu_Manix
You can use indexOf() for find a word too.
And for the substring, the good syntax is :
code:
var a = "abc";
var b = a.substring(1);
(...)
I give him only the good syntax of substring...
But ".substr()" work too ?
There is no 'good' and 'wrong' syntax.
substr and
substring are two different functions. They do not work the same way. Each has his own specific uses.
quote:
Originally posted by deAd
code:
var sSubString = sString.substr(1,3); // The first parameter is the index to start at. It is zero-based. The second is how many characters to include. If you leave out the second, it will just cut off however many characters you specify in the first parameter.
nope... leaving out the second parameter will not cut off anything; it will return the remainder of the whole string.
-------------------------
Syntax: stringvar.substr(start [, length ])
- stringvar
Required.
A string literal or String object from which the substring is extracted.
- start
Required.
The zero-based starting position of the desired substring.
- length
Optional.
The number of characters to include in the returned substring.
If zero or negative, an empty string is returned.
If not specified, the substring continues to the end of stringvar.
- Example
var s = "The rain in Spain falls mainly in the plain.";
return s.substr(12, 5); // Returns "Spain".
return s.substr(24); // Returns "mainly in the plain.".
Syntax: stringvar.substring(start, end)
- stringvar
Required.
A string literal or String object from which the substring is extracted.
- start
Required.
The zero-based starting position of the desired substring.
- end
Required.
The zero-based position indicating the end of the substring.
- Remarks
The substring method returns a string containing the substring from start up to, but not including, end.
The substring method uses the lowest value of start and end as the beginning point of the substring. For example, strvar.substring(0, 3) and strvar.substring(3, 0) return the same substring.
If either start or end is NaN or negative, it is replaced with zero.
The length of the substring is equal to the absolute value of the difference between start and end. For example, the length of the substring returned in strvar.substring(0, 3) and strvar.substring(3, 0) is 3.
- Example
var s = "The rain in Spain falls mainly in the plain..";
return s.substring(12, 17); // Returns "Spain".
return s.substring(17, 12); // Returns "Spain".
return s.substring(-10, 3); // Returns "The".
---------
In this post, you can see the 3 practical and different examples of the different uses:
CookieRevised's reply to "[I help them] VB2JS":
quote:
Snippet from original posting from CookieRevised
// Returns a string containing a specified number of characters from the left side of a string.
function Left(s, length) { return s.substring(0, length) }
// Returns a string containing a specified number of characters from the right side of a string.
function Right(s, length) { return s.substr(s.length-length) }
// Returns a string containing a specified number of characters from a string (length parameter is optional).
function Mid(s, start, length) { return s.substr(--start, length) }
Left() uses the substring method
Right() uses the substr method
Mid() uses the substr method with optional length parameter.