Simple summary of String string type _ basics in JavaScript

Source: Internet
Author: User
Tags character set

The string type represents a sequence of characters consisting of 0 or more 16-bit Unicode characters, that is, a string. Strings in ECMAScript are immutable, that is, once a string is created, their values cannot be changed. To change the string that a variable holds, first destroy the original string (which occurs in the background), and then populate the variable with another string containing the new value
Character literal/escape sequence:

\ n
-linefeed \
\ r
-tab-return \b Space
\f the page break \
slash
\ ' single quote
\ ' Double quote
\xnn 
\unnn

String Conversions: Two methods
1.toString (): Only null and undefined do not have this method.

Each string also has a ToString () method that returns a copy of the string. In most cases, calling the ToString () method does not have to pass arguments, but when you call the ToString () method of a value, you can pass a parameter: the cardinality of the output value. The only way to do this is to return a string representation of the corresponding value.

var num = ten;
Console.log (Num.tostring ());//"Ten"
Console.log (num.tostring (2));//"1010"

2.String (): Applicable to all types, follow these rules

If the value has the ToString () method, call the method (without parameters) and return the corresponding result
Returns "NULL" if the value is null
Returns "Undefined" if the value is undefined

Example

A string is a set of immutable ordered sequences of 16-bit values, each of which is usually from a Unicode character set.

var i = "ABCDEFG"; 

In a JavaScript string, a backslash \ has a special purpose, and a backslash, followed by a character, no longer denotes their literal meaning, or an escape character, which represents a line break.

' You\ ' re right, it can\ ' t be a quote '

One of the built-in features of JavaScript is string concatenation:

msg = "Hello," + "world";

The length property of the string allows you to view the lengths of the string:

S.length

In addition to the length property, the string also provides a number of methods that can be invoked:

var s = "Hello, world"//define a string
s.charat (0)   //=> "H" first character
S.charat (s.length-1)//=> "D" last character
s . substring (1, 4)//=> "ell" 第2-4个 character
S.slice (1,4)//=> "ell" ditto
S.slice ( -3)//=> "Rld": last three characters
s.in Dexof ("L")//=> 2 character L first appeared position
S.lastindexof ("L")//=> 10: Character L last occurrence
s.indexof ("L", 3)//=> at position 3 and after first The position of the character L is
s.split (",")//=> ["Hello", "world"] is divided into substrings
s.replace ("H", "H")//=> "Hello, World": Full character replacement 
   s.touppercase ()//=> "HELLO World"

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.