Home >Web Front-end >JS Tutorial >Detailed explanation of commonly used string manipulation methods in JavaScript

Detailed explanation of commonly used string manipulation methods in JavaScript

小云云
小云云Original
2018-03-20 09:50:201539browse

This article mainly introduces to you the common operation methods of strings in JavaScript and js special characters. Friends who need it can refer to it. I hope it can help everyone.

Let’s take a look at the common operation methods of js strings. The specific contents are as follows:

charAt()                                                                                       # #toupperCase() Converts all characters of the string to uppercase letters;

indexOf() Returns the position of the first occurrence of a specific string in the string

substring() Returns the position of the string A certain substring

slice()                                                        -                                                                                                                                                                               ininginginginginginginginginginging to is to come from the last character from the penultimate character to - 1. Multiple strings are combined into one string

replace() Replace a certain substring in the string with a specific string

split() Split a string into multiple A string, you can specify the delimiter

match() Use regular expression to search the target substring

search() Use regular expression to search the target substring (the index is returned)


ps: JavaScript Special Characters

You can use backslashes in JavaScript to add special characters to text strings.

Insert special characters

Backslashes are used to insert ellipses, newlines, quotation marks, and other special characters in text strings. Please look at the following JavaScript code:

var txt="We are the so-called "Vikings" from the north."
document.write(txt)

In JavaScript, strings use single quotes or double quotes to start or end. This means that the above string will be truncated to: We are the so-called.


To solve this problem, you must add a backslash (\) before the quotation mark in "Viking". This converts each double quote into a literal string.

var txt="We are the so-called \"Vikings\" from the north."
document.write(txt)

Now JavaScript can output the correct text string: We are the so-called "Vikings" from the north.


Here is another example:

document.write ("You \& me are singing!")

The above example will produce the following output:


You & me are singing!

The following table lists the remaining special characters, which can be added to text strings using backslashes:

Code Output
\' Single quotes
\" Double quotes
\& and ampersand
\\ Backslash
\n Line break
\r Carriage return character
\t Tab character
\b Backspace character
\f Form feed character

Related recommendations:

Detailed explanation of strings in JavaScript

Detailed explanation of match and replace examples of strings in JavaScript

Summary of common operations on strings in JavaScript

The above is the detailed content of Detailed explanation of commonly used string manipulation methods in JavaScript. 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