Use of the JavaScript substr () string truncation Function

Source: Internet
Author: User

Substr definition and usage
The substr () method extracts a specified number of characters starting with the start subscript from a string.

Syntax
StringObject. substr (start, length)

Parameters Description
Start Required. The starting subscript of the substring to be extracted. It must be a numerical value. If it is a negative number, this parameter declares the position starting from the end of the string. That is to say,-1 refers to the last character in the string,-2 refers to the second to last character, and so on.
Length Optional. The number of characters in the substring. It must be a numerical value. If this parameter is omitted, the string from the start position of the stringObject to the end is returned.

Description
If the length is 0 or negative, an empty string is returned. If this parameter is not specified, the substring will continue to the end of stringvar.

Example
The following example demonstrates the substr method usage.

The first truncates the first character of the string.


[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
Result: j


[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

It refers to the truncation of a string starting from 12th characters and being 5 characters in length.

In this example, we will use substr () to extract some characters from the string:


[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Output:

Lo world!

Example 2

In this example, we will use substr () to extract some characters from the string:

[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Output:

Lo worl

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.