search
HomeWeb Front-endJS TutorialThe definition and usage of substring and substr in js_javascript skills

1.substring method

Definition and usage

The

substring method is used to extract characters between two specified subscripts in a string.

Grammar

stringObject.substring(start,stop)

Parameter Description
start Required. A nonnegative integer that specifies the position in stringObject of the first character of the substring to be extracted.
stop Optional. A nonnegative integer that is one position in the stringObject that is one more than the last character of the substring to be extracted. If this parameter is omitted, the returned substring will go to the end of the string.

Return value

A new string value containing a substring of stringObject whose content is all characters from start to stop-1, and whose length is stop minus start.

Description

The substring returned by the substring method includes the characters at start, but does not include the characters at end.
If start and end are equal, then this method returns an empty string (that is, a string of length 0).
If start is greater than end, then this method will exchange these two parameters before extracting the substring.
If start or end is negative, then it will be replaced with 0.

2.substr method

Definition and usage

The

substr method is used to return a substring of the specified length starting from the specified position.

Grammar

stringObject.substr(start [, length ])

Parameter Description
start Required. The starting position of the desired substring. The first character in the string has index 0.
length Optional. The number of characters that should be included in the returned substring.

Description

If length is 0 or negative, an empty string will be returned.
If this parameter is not specified, the substring will be continued to the end of stringObject.

3. Example

Copy code The code is as follows:

text/javascript">
function Demo(){
var str,str;
var s = "Hello Word";

str = s.substring (0, 3); // Get substring.
console.log (str); // ===== & gt; Hel

STR = S.Substr (0,3);
console.log (sTR); // = ====>Hel
} }

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Java如何使用StringBuilder类的substring()函数截取字符串的子串Java如何使用StringBuilder类的substring()函数截取字符串的子串Jul 24, 2023 pm 12:13 PM

Java如何使用StringBuilder类的substring()函数截取字符串的子串在Java中,我们经常需要处理字符串的操作。而Java的StringBuilder类提供了一系列的方法,方便我们对字符串进行操作。其中,substring()函数可以用于截取字符串的子串。substring()函数有两种重载形式,分别是substring(intstar

PHP返回字符串第一个字符的 ASCII 值PHP返回字符串第一个字符的 ASCII 值Mar 21, 2024 am 11:01 AM

这篇文章将为大家详细讲解有关PHP返回字符串第一个字符的ASCII值,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PHP返回字符串第一个字符的ASCII值引言在php中,获取字符串第一个字符的ASCII值是一个常见的操作,涉及到字符串处理和字符编码基础知识。ASCII值用于表示字符在计算机系统中的数字值,对于字符比较、数据传输和存储至关重要。过程获取字符串第一个字符的ASCII值涉及以下步骤:获取字符串:确定要获取ASCII值的字符串。它可以是变量、字符串常量

PHP返回一个字符串在另一个字符串中开始位置到结束位置的字符串PHP返回一个字符串在另一个字符串中开始位置到结束位置的字符串Mar 21, 2024 am 10:31 AM

这篇文章将为大家详细讲解有关PHP返回一个字符串在另一个字符串中开始位置到结束位置的字符串,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PHP中使用substr()函数从字符串中提取子字符串substr()函数可从字符串中提取指定范围内的字符。其语法如下:substr(string,start,length)其中:string:要从中提取子字符串的原始字符串。start:子字符串开始位置的索引(从0开始)。length(可选):子字符串的长度。如果未指定,则提

如何使用Java中的String.substring()方法获取子字符串?如何使用Java中的String.substring()方法获取子字符串?Nov 18, 2023 am 08:07 AM

如何使用Java中的String.substring()方法获取子字符串?Java中的String类提供了一个非常有用的方法substring(),可以用于获取字符串的子字符串。它允许我们从一个字符串中选择一部分字符,并将其作为一个新的字符串返回。本文将介绍如何使用Java中的substring()方法,并提供一些代码示例。使用substring()方法非常

了解PHP中的substr()函数用于截取字符串了解PHP中的substr()函数用于截取字符串Nov 18, 2023 am 11:27 AM

了解PHP中的substr()函数用于截取字符串在PHP语言中,substr()函数是一个非常有用的字符串处理函数,它可以用于截取指定位置和长度的字符串片段。substr()函数接受三个参数:待截取的字符串、截取的起始位置和截取的长度。下面我们将详细介绍substr()函数的使用方法,并给出具体的代码示例。substr()函数的基本用法substr()函数的

PHP将字符串的首字母转换为小写PHP将字符串的首字母转换为小写Mar 21, 2024 pm 02:11 PM

这篇文章将为大家详细讲解有关PHP将字符串的首字母转换为小写,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。将PHP字符串的首字母转换为小写引言在php中,将字符串的首字母转换为小写是一个常见的操作。可以通过使用内置函数lcfirst()或字符串操作符strtolower()来实现。本指南将深入探讨这两种方法,并提供实例代码和最佳实践。方法1:使用lcfirst()函数lcfirst()函数专门用于将字符串的首字母转换为小写,而其余字符保持不变。其语法如下:st

使用PHP函数 "substr" 获取字符串的子串使用PHP函数 "substr" 获取字符串的子串Jul 24, 2023 pm 10:13 PM

使用PHP函数"substr"获取字符串的子串在PHP编程中,经常会遇到需要获取字符串的部分内容的情况。这时,我们可以使用PHP内置的函数"substr"来实现。本文将介绍如何使用"substr"函数获取字符串的子串,并提供一些代码示例。一、substr函数的基本用法substr函数用于从字符串中获取指定长度的子串。其基本语法如下:substr(

使用java的StringBuilder.substring()函数截取字符串的子串使用java的StringBuilder.substring()函数截取字符串的子串Jul 26, 2023 pm 11:45 PM

使用java的StringBuilder.substring()函数截取字符串的子串在Java编程中,经常需要对字符串进行操作,其中一种常见的操作就是截取字符串的子串。Java提供了多种方法来实现这一功能,其中StringBuilder类的substring()函数是一种简便且高效的方法。StringBuilder是Java中用于操作字符串的一个类,它提供了

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),