Learn from scratch front end JAVASCRIPT-3, JavaScript basics String String Introduction

Source: Internet
Author: User

1: String

Any data type in JS can be viewed as an object. So string is both the basic data type and the object.

2: declaration string

Basic data type: var sStr = ' string ';

Method of the object: var ostr = new String (' string ');

        //count the number of occurrences of each character, the result shows a 2, B 1, c 2, D1, remove the duplicate characters, so that the results are displayed abcdfgj.         //var defines a variable assignment string that accesses a single character in the form of an object [] IE8 above support        varStr= "ABCDAFGCJ"; varArr={}; varNewstr= "";  for(vari = 0; i < str.length; i++) {            if(Arr[str[i]]) {Arr[str[i]]++; }             Else{Arr[str[i]]=1; }        }         for(Newarrincharr) {Newstr+=newarr+ ":" +arr[newarr]+ ","; } document.write (NEWSTR)//String.charat to get the value of the string corresponding to the location of the lower version browser        varStr= "ABCDAFGCJ"; varArr={}; varNewstr= "";  for(vari = 0; i < str.length; i++) {            varCode=Str.charat (i); if(Arr[code]) {Arr[code]++; }             Else{Arr[code]=1; }        }         for(Newarrincharr) {Newstr+=newarr+ ":" +arr[newarr]+ ","; } document.write (NEWSTR)
declaring strings using demo

3: String Property

1.length: calculates the length of the string (without distinction between Chinese and English).

2.constructor: the constructor for the object.

4: String Method

Serial number Method name Role
1 Mystr.charat (num) Returns the character at the specified position
2 Mystr.charcodeat (num) Returns the Unicode (a pattern of character encoding) encoding of the character at the specified position.
3 String.fromCharCode () String means that it cannot be invoked with a string name of its own definition, such as defining a variable string var str= ""; a call can be defined only with a string. Accepts one or more specified Unicode values, and then returns one or more strings. (converts Unicode encoding to a string).
4 Mystr.indexof () Returns the position of the first occurrence of a specified string in a string. If the string value that you want to retrieve does not appear, the method returns-1. The second parameter specifies the starting position at which to start the lookup.
5 Mystr.lastindexof () Returns the location of the last occurrence of a specified string value, which returns 1 if the string value to retrieve does not appear. The second parameter specifies the starting position of the start lookup, and only a positive number can be specified.
6 Mystr.match () Retrieves the specified value in the string, and the returned value is an array. Returns null if no match is reached. With the regular to use.
7 Mystr.search () Returns the location that appears, not finding return-1. With the regular to use.
8 Mystr.replace ("String to be replaced", "replaced string") Replaces some characters in a string with some other characters. Used in conjunction with the regular.
9 Mystr.slice (Start,end) All strings from the specified start position to the end position (excluding the end position). If you do not specify an end position, the end is taken from the specified start position. Note that Mystr.slice () is similar to Myarr.slice ().
10 Mystr.substring (Start,end) All strings from the specified start position to the end position (not included). If you do not specify an end position, the end is taken from the specified start position.
11 SUBSTR (Start,length)

Takes a string of the specified length from the specified position. If no length is specified, the end is taken from the specified starting position. ECMAscript did not standardize the method and therefore objected to its use .

If start of substr is specified as a negative number, the parameter declares the position from the end of the string. That is,-1 refers to the last character in the string, 2 refers to the second-lowest character, and so on.

Slice (start,end) vs substring (Start,end): the slice parameter can be a negative number, or, in the case of a negative number, the end of the string, starting with 1. when the substring parameter is negative, it is automatically converted to 0.

12 Split ("Split position", [specified length]) Splits a string into an array.
13 toLowerCase () Used to convert a string to lowercase.
14 toUpperCase () Converts a string to uppercase.

5:ascii code and Character set

Ascii:american Standard code for Information Interchange, U.S. Information Interchange standards codes.

Unicode encoding:

Unicode (Uniform Code, universal Code, single Code) is an industry standard in the field of computer science, including character set, encoding scheme, etc. Unicode is created to address the limitations of traditional character encoding schemes, which set a uniform and unique binary encoding for each character in each language to meet the requirements of cross-language, cross-platform text conversion and processing. Unicode is currently commonly used by UCS-2, which encodes a character in two bytes.

such as the Chinese character "Sutra" encoding is 0X7ECF, note that the character code is generally used in hexadecimal notation, in order to distinguish with the decimal, hexadecimal to 0x, 0x7ecf conversion to decimal is 32463,ucs-2 with two bytes to encode characters, two bytes is 16 bit binary, 2 is 16 times equal to 65536, so UCS-2 can encode up to 65,536 characters.

GBK Code:

GBK full Name "Chinese character Code Extension code" (GBK "GB", "expand" the first letter of Hanyu Pinyin, English name: Chinese Internal Code specification). GBK downward is compatible with GB2312 encoding, which supports the ISO 10646.1 International standard upward, which is a connecting product of the former transition process to the latter.

UTF-8 Code:

UTF-8 (8-bit Unicode Transformation Format) is a variable-length character encoding for Unicode, also known as the Universal Code. UTF-8 encodes Unicode characters in 1 to 4 bytes. The same page can be used to display Chinese simplified traditional and other languages (such as English, Japanese, Korean).

Learn from scratch front end JAVASCRIPT-3, JavaScript basics String String Introduction

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.