More "Try it Yourself" examples below.Bạn vẫn xem: Get a part of string javascript
Definition & Usage
The substring() method extracts the characters from a string, between two specified indices, và returns the new sub string.
Bạn đang xem: Java list find index by value
This method extracts the characters in a string between "start" & "end", not including "end" itself.
If either "start" or "end" is less than 0, it is treated as if it were 0.
Xem thêm: Định Nghĩa Của Kleptocracy Là Gì ? Định Nghĩa, Ví Dụ, Giải Thích
Note: The substring() method does not change the original string.
Browser Support
Method substring() Yes Yes Yes Yes YesSyntax
string.substring(start, end)Parameter Values
Parameter description start Required. The position where lớn start the extraction. First character is at index 0 end Optional. The position (up to, but not including) where to end the extraction. If omitted, it extracts the rest of the stringTechnical Details
Return Value: A new String containing the extracted characters JavaScript Version: ECMAScript 1More Examples
Example
Begin the extraction at position 2, & extract the rest of the string:
var str = "Hello world!"; var res = str.substring(2); Try it Yourself »Example
If "start" is greater than "end", it will swap the two arguments:
var str = "Hello world!";var res = str.substring(4, 1); Try it Yourself »Example
If "start" is less than 0, it will start extraction from index position 0:
var str = "Hello world!"; var res = str.substring(-3); Try it Yourself »Example
Extract only the first character:
var str = "Hello world!"; var res = str.substring(0, 1); Try it Yourself »Example
Extract only the last character:
var str = "Hello world!"; var res = str.substring(str.length - 1, str.length); Try it Yourself » ❮ Previous JavaScript String Reference Next ❯ màu sắc PICKER
like US
Get certifiedby completinga course today!
darkedeneurope.com CERTIFIED . 2021 Get started CODE game
Play Game
Certificates HTML CSS JavaScript Front end Python SQL và more REPORT ERROR forums ABOUT siêu thị ×Report Error
If you want khổng lồ report an error, or if you want khổng lồ make a suggestion, vì chưng not hesitate to send us an e-mail:
helpdarkedeneurope.com
×Thank You For Helping Us!
Your message has been sent to lớn darkedeneurope.com.