Summary of JavaScript string methods

Source: Internet
Author: User
Tags string methods

Single quotation marks inside the string can use double quotes, double quotation string inside can also use single quotation marks

"Hello ' world '" ' Welcome "to" JS "

Second, multirow and escape

If you want to use single quotation marks (or double quotes inside a double-quoted string) inside a single-quote string, you must precede the inner single quotation mark (or double quotation mark) with a backslash to escape.

var str = "Did you \" love\ "Me"var str = ' Do you \ ' love\ ' me '

The string default can only be written in one line, the number of lines will be error.

'ABC'//  syntaxerror:unexpected token illegal

The above code divides a string into three lines, and JavaScript will error

Method One: If the long string must be divided into multiple lines, you can use a backslash at the end of each line.

var str = "welcome come to\     Chongqing.welcome to    Chongqing"

The above code indicates that, after adding a backslash, the original string written on a line, can be divided into multiple lines, the effect is exactly the same as writing in the same line. Note that the backslash must be followed by a newline character, not other characters (such as spaces), or it will be an error.

Method Two: The Join operator ( + ) can concatenate multiple single-line strings to simulate a multiline string.

var str = "Welcome come to" +    "chongqing.welcome to" +    "Chongqing"

Third, length calculation str.length

var str = "Hello"; Console.log (str.length); // 5

Four, string interception

1, substr (A, b) the first parameter is the starting position, the second parameter is the length of the Intercept

var str = "Welcome to JavaScript"Console.log (Str.substr (1,3))//ELC

2, substring (A, b) the first parameter is the starting position, the second parameter is the end position, the interval is [a, b]

var str = "Welcome to JavaScript"Console.log (str.substring (1,3))//el

3, Slice (A, B) ibid.

Five, string split into an array split (?)

var str = "We lc ome"console.log (Str.split (""))//["We", "LC", "ome"]

Six, query substring

1, indexOf (?) starts from the beginning of the string, finds the corresponding coordinates and exits, cannot find return-1 and exits ( does not support regular )

var str = "We lc ome"console.log (Str.indexof ("E"))//1

2, lastIndexOf (?) Search from the end of the string, find the corresponding coordinates returned and exit, cannot find return 1 and exit ( not supported )

var str = "We lc ome"console.log (Str.lastindexof ("E"))//8

Seven, string matching

1. Match (?) the function is called on a string and accepts a parameter (which can be a regular ).返回匹配的数组,否则返回null

var str = "Hello my World"; var s3 = Str.match (/o/g); Console.log (S3)//["O", "O"] 

2, search (?) is similar to indexof (), find a matching string to return the corresponding coordinates, cannot find return-1, the difference is that search can use regular expressions, indexof cannot use regular expressions, If you are looking for a string with some characteristics (such as looking for a string that starts with an L and followed by a number), then indexof () has nothing to do with the regular expression and the search () method.

var str = "Hello my World"; var s1 = str.search (/o/g);   Console.log (S1)//4

The trim () method is used to remove spaces before and after a string

var str= "   Hello World   "; Console.log (Str.trim ()); //

Nine, replace (A, B) method, the first parameter is the string to be replaced, the second argument is a replacement string (the first argument is a character, only the most recent replacement, the first parameter if it is a regular, you can find and replace all the matching string)

The first argument is a string
var str = "Hello my World"; var s2 = str.replace (' o ', ' your '); Console.log (S2)//hellyour My World
The first parameter is a regular form
var str = "Hello my World";
var s2 = str.replace (/o/g, ' your ');
Console.log (S2)//hellyour my Wyourrld

X. Uppercase and lowercase conversions

var str = "Hello";    Str.touppercase (); // HELLO    Str.tolowercase (); // Hello

Summary of JavaScript string methods

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.