JS in six types of data (v)--string

Source: Internet
Author: User

The string type is used to represent a sequence of characters consisting of 0 or more 16-bit Unicode characters, the string. The string can be represented by double quotation marks (") or single quotation marks ('), so the following two strings are written in a valid way:

var firstname= "Marry";    var firstname= ' Jane '; Unlike double and single quotes in PHP, which affect how strings are interpreted, there is no difference between the two syntactic forms in ECMAScript. A string expressed in double quotation marks is exactly the same as a string in single quotation marks. However, strings that begin with quotation marks must also end with double quotes, and strings that begin with single quotation marks must end with single quotes. For example, the following string notation causes syntax errors: var firstname= "Nicholas"; Syntax error (left and right quotes must match) 1. Character literalThe String data type contains some special character literals, also called escape sequences, that represent nonprinting characters, or characters that have other uses. These character literals are shown in the following table:


These character literals can appear anywhere in the string, and will be parsed as a character, as shown in the following example:


var text= "This was the letter sigma: \u03a3.";     The variable text in this example has 28 characters, with a 6-character transfer sequence representing 1 characters. The length of any string can be obtained by accessing its long property, for example: Alert (text.length);//Output 28     The number of characters returned by this property includes the number of 16-bit characters. If the string contains double-byte characters, the length property may not accurately return the number of characters in the string. 2. Characteristics of StringsThe strings in the ECMAScript are immutable, that is, once the strings are created, their values cannot be changed.        To change the string saved by a variable, first destroy the original string, and then populate the variable with another string containing the new value, for example: Var lang= "Java";     lang=lang+ "Script"; The variable lang in the example above starts with the string "Java". The second line of code redefined the value of Lang as a combination of "Java" and "Script", or "JavaScript." This is done by first creating a new string that can hold 10 characters, and then populating the string with "Java" and "script", and the final step is to destroy the original string "Java" and the string "script" because these two strings are useless. This process occurs in the background, and this is why it is very slow to stitch strings in some older browsers (such as FIREFOX,IE6 with versions below 1.0). But later versions of these browsers have solved this inefficient problem. 3. Converting to a string    There are two ways to convert a value to a string. The first is the ToString () method, which uses almost every value. The only way to do this is to return the string representation of the corresponding value. Take a look at the following example:         var age=11;        var ageasstring= Age.tostring ();  //string "All"         var fount=true;        var foundasstring= Found.tostring ()///String "True"       numeric, Boolean, object and string values (yes, each string also has a toString () method, The return value of the method returns a copy of the string) with the ToString () method. However, the null and undefined values do not have this method.     In most cases, calling the ToString method does not have to pass arguments. However, when you call the ToString () method of a number, you can pass a parameter: the cardinality of the output value. By default, the ToString () method returns a string representation of a numeric value in decimal format. By passing the cardinality, toString () outputs a string value expressed in binary, octal, hexadecimal, or any other valid binary format. Here are a few examples:         var num=10;        alert ( Num.tostring ());  //"Ten"         alert (num.tostring (2));  //"1010"         alert (num.tostring (8));  //"        alert (num.tostring (10));  //"10 " Alert (num.tostring (16)); A
This example shows that by specifying the cardinality, the ToString () method changes the value of the output. The value 10, depending on the cardinality, can be converted to a different numeric format at output.    Note that the default (no parameter) output value is the same as the output value for the specified cardinality 10 o'clock. You can also use the Transform function string (), which can convert any type of value to a string, without knowing that the value to be converted is null or undefined. The String () function follows the following translation rules: If the value has the ToString () method, the method is called (without arguments) and the corresponding result is returned, or "null" if the value is null, and if the value is undefined, it is returned under "undefined"       See a few more examples: Var value1=10;       var value2=true;        var value3=null;    Alert (String (value1));    "Ten" alert (String (value2));    "True" alert (String (VALUE3)); "NULL"Alert (String (VALUE4)); "Undefined"
There are 4 values converted: Numeric, Boolean, NULL, and undefined. The result of the conversion of numeric and Boolean values is the same as the result of calling the ToString () method. Because null and undefined do not have the ToString () method, the string () function returns the literal of both values.





Welcome to the public number: Wcs290130--javascript those things. JavaScript Technology Exchange Group: 344292264.

JS in six types of data (v)--string (EXT)

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.