JavaScript Basics--string_jsp Programming

Source: Internet
Author: User

Learn the basics is to learn a language foundation, the following string, together to learn.

Unlike string and string, string is a constructor, and string is a type of variable. (String is an instance of string)

Use after declaring a string, var str = "Wo shi yi ge hao xue sheng"

is included

① determines whether a string is contained in another string, contains return True, does not contain a return of false Str.includes (Searchstr,formindex) Searchstr the string that the query contains Formindex where to start.

Str.includes (' wo ')//true

Cutting string

① gets a section of the string, returns a new string Str.slice (Start,end) If it is negative, it is treated as start+str.length, end.

 Str.slice ( -2)//ng

② the string as an array, returning an str.split (separator, howmany) separator can be a string or regular expression, Howmany is the length of the array after the cut

 Str.split (/i/g)//["Wo sh", "y", "ge Hao Xue Sheng"]

③ gets a certain number of characters from the beginning of the string subscript, returns a new string Str.substr (start,length) Start can be negative, if negative is the inverse index

Str.substr ( -2)//ng

④ Extracts character str.substring (start,end) start,end nonnegative integers between two specified subscripts

 Str.substring (1,2)//o

Find the location of the string |

① find out if a character exists, where it returns the first occurrence, no return -1,fromindex is an optional option, and, if so, is checked from an index position str.indexof (SEARCHSTR,FROMINDEX)

 Str.indexof (' Shi ')//return first occurrence position, 3
 str.indexof (' Shi ', 8)//Return-1

② returns the last occurrence of the specified string, searching backwards, fromindex as the starting index Str.lastindexof (searchstr,fromindex)

Str.lastindexof (' Shi ')//return to the last occurrence position, 3
str.lastindexof (' Shi ', 2)//return-1

③ query the matching of regular expressions within a string, returns an array, cannot find a return null, and if the argument is not regular, it is forcibly converted to a regular str.match (REGEXP)

 Str.match (/[1-9]/g)//null
 str.match (/\d/g)//["W", "O", "s", "H", "I", "Y", "I", "G", "E", "H", "A", "O", "X", "U", " E "," s "," H "," E "," N "," G "]

④ returns the index of the first character of the substring that matches the regular expression, and does not return-1 str.search (REGEXP)

 Str.search (/wo/g); 0

About the encoding of

① returns the encoding of a location Str.charcodeat (index)

 Str.charcodeat (1)//The code that returns the position of index 1, 111

② creates a string with some encoding String.fromCharCode ()

 String.fromCharCode (111)//compiled string is O

About the location of

① returns the character of a position Str.charat (index)

 Str.charat (1)//Returns the character of the position indexed to 1, O

Replace text

①. Replace the text that matches the regular expression str.replace (REGEXP,STR)

Str.replace (/hao/g, ' Huai ')//wo shi Yi ge huai xue

②. Remove the space from the two paragraphs of the string and return the new string Str.trim ()

and write Concat () connection string, I can think of the only advantage is that I write a few + number connectors, said Concat () thought of repeat () how to do ... str.repeat (num)

I hope this article will help you with JavaScript programming.

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.