Home  >  Article  >  Web Front-end  >  A collection of common methods for intercepting strings in JS

A collection of common methods for intercepting strings in JS

韦小宝
韦小宝Original
2018-01-26 10:27:062123browse

Strings in js are a very long-used data type, both in our actual development and in daily practice, and we often encounter fewer suffixes or more suffixes in the js strings we need. A suffix, which requires manipulation of strings. Let’s take a look at how to intercept strings! How to cut off the redundant part of the string!

We can easily do it using functions. Let’s take a look at which functions can do it!

Function: split()
Function: Use a specified delimiter to split a string and store it in an array
Example:

var str = "jpg|bmp|gif|ico|png";
var arr = str.split("|");

arr is an array containing the character values ​​"jpg", "bmp", "gif", "ico" and "png"

Function: Join()
Function: Combine an array into a string using the delimiter of your choice
Example:

var arry = new Array('jpg','bmp','gif','ico','png'); 
var str = arry.join('|'); 
//结果是jpg|bmp|gif|ico|png

Function: indexOf()
Function: Return the subscript of the first character matching the substring in the string

Example:
var myString = "JavaScript"; 
var w = myString.indexOf("v"); // w = 2 
var x = myString.indexOf("s"); // x = 4 
var y = myString.indexOf(”Script”); // y = 4 

var z = myString.indexOf(”key”); // z = -1

Function: substring()
Function: String interception, for example, if you want to get "Minidx" from "MinidxSearchEngine", you need to use substring(0,6)

##Syntax stringObject.substring(start,stop)

Parameter descriptionstart 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 valueA new string value contains a substring of stringObject, and its content is from start to stop-1 All characters whose length is stop minus start.

DescriptionThe 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 with a length of 0).
If start is greater than end, then this method will exchange the two parameters before extracting the substring.
If start or end is negative, then it will be replaced with 0.

Function: substr ()Definition and usage
The substr method is used to return a substring of the specified length starting from the specified position.

SyntaxstringObject.substr(start [, length])

Parameter descriptionstart 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.

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

There are three commonly used character interception functions in js: slice(), substring(), and substr().

Related recommendations:


Detailed explanation of javaScript string tool class StringUtils

This article mainly introduces the javaScript string tool class StringUtils in detail .

Detailed introduction to JavaScript strings

Strings in JavaScript are represented by characters enclosed in '' or "".


The above is the detailed content of A collection of common methods for intercepting strings in JS. For more information, please follow other related articles on the PHP Chinese website!

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