A detailed description of string encoding and string methods in JavaScript

Source: Internet
Author: User

The string in JS is an array of classes, using the UTF-16 encoded Unicode character set, which means that each character of the string can be obtained by subscript, and each string is a sequence of 16-bit values in memory. JS operations on strings are performed on 16-bit values, not characters.

In JS, the first character after "\" will not be resolved to a character, which is the escape character of JS:

\o         nul character \b         backspace \ t          horizontal tab \ n         newline character \v         vertical tab \f          page break \ r          carriage return \ "          double quotation mark \"            Apostrophe or single quote \ \           Backslash \xxx The Latin-1 character         specified by the two-digit hexadecimal number xx\uxxxx the         Unicode character specified by the four-bit hexadecimal number xxxx 
    var str= "Pomelott is good\nyes he's!" ;    Console.log (STR1);

The console output is as follows:

!

JS Common String Method usage explained:

    varS= "Hellow,pomelott"; Console.log (S.charat (0));//hConsole.log (S.charat (s.length-1));//T    //substring (start, end)    //1. Starting from subscript 1 (including) to subscript 4 end (not included)    //2. SUBSTRING () parameter does not support negative numbersConsole.log (s.substring (1,4));//"ell"    ///slice (Start, end)    //1. If the starting digit is negative, start from the countdown;    //2. End bit number optional, default means cut from start to end of stringConsole.log (S.slice (1,4));//"ell"Console.log (S.slice (-3));//"Ott"    //s.indexof ("x") x the first occurrence of the position, does not exist then returns-1Console.log (S.indexof ("L"));//2Console.log (S.indexof ("z"));//-1Console.log (S.indexof ("O", 6));//o position first appearing after position 6, no return-1    //s.lastindexof ("x") x the last occurrence of the position, does not exist then returns-1Console.log (S.lastindexof ("O"));// AConsole.log (S.lastindexof ("z"));//-1Console.log (S.split (","));//Convert a string to an array based on ","Console.log (S.replace ("O", "O"));//Hellow,pomelott Replace the first "O" with "O"Console.log (S.touppercase ());//convert full text to uppercase    varStr1= "Hellow,"; varStr2= "World";  Console.log (Str1.concat (str2)); //Hellow,world String Connection    //The Search () method is used to retrieve the substring specified in a string, or to retrieve a substring that matches a regular expression. Console.log (S.search (/pomelo/));//7 exists then returns the first word Poute, does not exist then returns-1    //The match () method can be retrieved within a string based on a regular expression, or, if present, returns the retrieved object, otherwise nullConsole.log (S.match (/pomelo/));//["Pomelo", Index:7, Input: "Hellow,pomelott"]Console.log (typeofS.match (/pomelo/));//ObjectConsole.log (S.match ("Pop"));//NULL

A detailed description of string encoding and string methods in JavaScript

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.