JavaScript Concise tutorial (2) string

Source: Internet
Author: User

The escape character \ can escape many characters, such as \ n for newline, \ t for tabs, and the character \ itself to be escaped, so \ \ is the character that \ \.
ASCII characters can be #形式的十六进制表示 in \x#, for example:

[JavaScript]Plain Text view copy code ?
001 ‘\x41‘; // 完全等同于 ‘A‘


You can also use \u### #表示一个Unicode字符:

[JavaScript]Plain Text view copy code ?
001 ‘\u4e2d\u6587‘; // 完全等同于 ‘中文‘


Because multiline strings are more cumbersome to write, the newest ES6 standard adds a multi-line string representation, denoted by ' ... ':

[JavaScript]Plain Text view copy code ?
001002003 `这是一个多行字符串`;



Common actions for strings are as follows:

[JavaScript]Plain Text view copy code ?
001002 vars = ‘Hello, world!‘;s.length; // 13

To get a character at a specified position in a string, use an array-like subscript operation with the index number starting at 0:

[JavaScript]Plain Text view copy code ?
001002003004005006007 vars = ‘Hello, world!‘;s[0]; // ‘H‘s[6]; // ‘ ‘s[7]; // ‘w‘s[12]; // ‘!‘s[13]; // undefined 超出范围的索引不会报错,但一律返回undefined

It is important to note that the string is immutable, and there is no error if you assign a value to an index of the string, but it does not have any effect:

[JavaScript]Plain Text view copy code ?
001002003 vars = ‘Test‘;s[0] = ‘X‘;alert(s); // s仍然为‘Test‘

JavaScript provides some common methods for strings, note that calling these methods does not alter the contents of the original string, but instead returns a new string:
toUpperCase
toUpperCase () turns a string all uppercase:

[JavaScript]Plain Text view copy code ?
001002 vars = ‘Hello‘;[/size][size=3]s.toUpperCase(); // 返回‘HELLO‘


toLowerCase
toLowerCase () turns a string all lowercase:

[JavaScript]Plain Text view copy code ?
001002003 vars = ‘Hello‘;varlower = s.toLowerCase(); // 返回‘hello‘并赋值给变量lowerlower; // ‘hello‘


IndexOf
IndexOf () searches for the location where the specified string appears:

[JavaScript]Plain Text view copy code ?
001002003 vars = ‘hello, world‘;s.indexOf(‘world‘); // 返回7s.indexOf(‘World‘); // 没有找到指定的子串,返回-1


Substring
SUBSTRING () returns the substring of the specified index interval:

[JavaScript]Plain Text view copy code ?
001002003 vars = ‘hello, world‘s.substring(0, 5); // 从索引0开始到5(不包括5),返回‘hello‘s.substring(7); // 从索引7开始到结束,返回‘world‘
http://www.sodu666.com/WuDiTianXia/http://www.ququer.org/

JavaScript Concise tutorial (2) string

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.