Javascript basics-String, javascriptstring

Source: Internet
Author: User

Javascript basics-String, javascriptstring

The foundation for learning a good language is the foundation for learning a good language. Below we will organize strings to encourage you to learn together.

String is different from string. String is a constructor and string is a type of variable. (string is an instance of String)

Declare a string and use it. var str = "wo shi yi ge hao xue sheng"

Include or not

① Determine whether a string is included in another string. If true is returned, false str. includes (searchStr, formIndex) is returned. If false is exclusive, the position where formIndex starts to be queried.

str.includes('wo') //true

Cut string

① Get a certain segment of the string and return a new string str. slice (start, end) start. If it is a negative number, it will be treated as start + str. length, the same as end.

 str.slice(-2) //ng

② Cut the string into an array and return an array str. split (separator, howator) separator which can be a string or a regular expression, and howator which is the length of the cut Array

 str.split(/i/g) //["wo sh", " y", " ge hao xue sheng"]

③ Obtain a certain number of characters starting from the string subscript and return a new string str. substr (start, length) start which can be a negative number. If it is a negative number, it is the reciprocal index.

str.substr(-2) //ng

④ Extract the str. substring (start, end) start and end non-negative integers between two specified subscripts.

 str.substring(1,2) //o

Locate the string | whether the string exists

① Check whether a character exists and the location where the first occurrence is returned.-1 is not returned. fromIndex is an optional option. If yes, check str from an index position. indexOf (searchStr, fromIndex)

Str. indexOf ('shi ') // returns the first occurrence location, 3 str. indexOf ('shi', 8) // returns-1

② Return the final position of the specified string. From the back-to-back search, fromIndex is the start index str. lastIndexOf (searchStr, fromIndex)

Str. lastIndexOf ('shi ') // returns the last occurrence location, 3str. lastIndexOf ('shi', 2) // returns-1

③ Query the matching of regular expressions in a string, return an array, and return null if not found. If the parameter is not a regular expression, it is forcibly converted to 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.-1 str. search (RegExp) is not returned)

 str.search(/wo/g); //0

Encoding

① Return the str. charCodeAt (index) encoding at a location)

Str. charCodeAt (1) // returns the encoding of the location where the index is 1, 111

② Use some encoding to create a String. fromCharCode ()

String. fromCharCode (111) // The compiled String is o

Location-related

① Return the character str. charAt (index) at a certain position)

Str. charAt (1) // returns the character at the position where the index is 1, o

Replace text

①. Replace the text str. replace (RegExp, str) that matches the Regular Expression)

str.replace(/hao/g,'huai') //wo shi yi ge huai xue sheng

②. Remove the spaces of the two strings and return the new string str. trim ()

There is also a concat () connection string. The only benefit I can think of is that I write a few less + number connectors. When it comes to concat (), I think about repeat .... str. repeat (num)

I hope this article will help you design javascript programs.

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.