JavaScript FAQ (vi)--string __java

Source: Internet
Author: User

Four, String

1. Quotation marks in strings (quotes in string)

Q: How do I insert quotes in a string?

a: The quotation marks in the string should be guided with a backslash (/). That way, JavaScript can tell whether these quotes are normal strings or delimiters for strings. Here is an example:

String1= ' it/' s five o/' clock! ';
string2= "<a href=/" index.htm/">";

Alternatively, if the string contains only single quotes, you can use double quotes as the delimiter for the string, and vice versa. Here is an example:

string1= "It ' s five o ' clock!";
String2= ' <a href= ' index.htm ' > ';

2. Convert numbers to strings (converting Numbers to Strings)

Q: In JavaScript, how do I convert a number to a string.

a: The simplest way is to add a numeric variable with an empty string, for example:

A = a + '//This converts a to string
B + = '  //This converts B to string

In the above example, the result string holds a decimal number, and if you want to convert to binary, octal, or hexadecimal (or any other binary) string, join (convert the other converting to Another Base).

3. Convert a string to a number (converting Strings to Numbers)

Q: How do I convert strings to numbers in JavaScript?

A: To convert a string to a number, you can use JavaScript's function parsefloat (convert to floating-point numbers) or parseint (converted to integers).

parsefloat Syntax: parsefloat (' string ')

Working mode:

The parsefloat argument must be a string or string expression. The return result of the parsefloat is the decimal number of the number within the string (or the first digit of the string). If a string cannot be resolved to a decimal number, the return value will vary by browser (0 or Nan).

Example (in the annotation is the transformation result):

parsefloat (' 1.45kg ')  //1.45
parsefloat (' 77.3 ')/    /77.3
parsefloat (' 077.3 ')   //77.3
parsefloat (' 0x77.3 ')  //0
parsefloat ('. 3 ')//      0.3
parsefloat (' 0.1e6 ')   //100000

parseintSyntax: parseint (' string ' [, base])

Working mode:

The first parameter of the parseint must be a string or a string expression. The return result of the parseint is an integer (or the first integer of the string) that the string contains. The second parameter (base), if any, gives the number in the string. The value of the parameter base can be any value from 2 to 36.

If there is only one parameter, the system of numbers is detected by the general syntax of JavaScript. A string that begins with a 0x or -0x is resolved to a hexadecimal number, and a string starting with 0 or 0 is resolved to the octal number, and the other is treated as a decimal number.

If the string argument cannot be resolved to an integer, the result of the return will vary depending on the browser (0 or Nan).

Example (the comment after the row is the transformation result):

parseint (' 123.45 ')  //123
parseint (' a ')      //
parseint (' 077 ', ten)  //
parseint (' 77 ', 8)    //  (= 7 + 7*8)
parseint (' 077 ')     //  (= 7 + 7*8)
parseint (' + ')   //119 (= 7 + 7*16)
parseint (' 0x77 '    )/ /119 (= 7 + 7*16)
parseint (' 099 ')     //0 (9 is isn't an octal digit)
parseint (' $ ', 8)    //0 or NaN, depend ing on the platform
parseint (' 0.1e6 ')   //0
parseint (' ZZ ', +)   //1295 (= 35 + 35*36)

4. SUBSTRING (substrings)

Q: How do I extract a substring from a string?

a: To extract a substring from a string, you can use the substring method:

String.substring (start, end)

Over here:

String is the string from which you want to extract substrings.

Start is a number that specifies the position of the character at which the substring starts. (The characters in the start position are also included in the substring.) )

End is a number that specifies the position of the character at which the substring ends. (characters in the end position are not included in the string.) )

Note that the position of the first character in the string is 0, and the last one is string.length-1.

Example:

' Hello '. substring (0,2)  //' He '
hello '. substring (0,4)  //' Hell '
hello '. substring (1,3)  //' El

5. Url-encoding

Q: How do I convert a string to url-encoding?

a: You can use the escape function to convert a string into a URL-encoded form to fit as a query string (or, generally, as part of a URL). This function works as follows: number, Latin alphabet and +-*/. @ @ remains unchanged, all other strings are converted to%xx escape form, where xx is the ASCII code of the original character. For example:

Escape ("It ' s me!")//Result:it%27s%20me%21

On a browser that can recognize Unicode (support for JavaScript1.3), the function escape can have more complex actions. If the input is a Unicode string, it is converted to the escaped Unicode form%uxxxx.

6. Reversal of Righteousness (unescape)

Q: How do I convert strings from url-encoding.

A: To convert a string from the Url-encoding form, you can use the JavaScript function unescape (string). This function works by: If the string contains an escape sequence such as%xx, where xx represents two hexadecimal digits, each escape sequence is replaced by an ASCII code of XX characters. Therefore, the string remains unchanged.

(on browsers that can recognize Unicode, function unescape can also handle escape sequences in%uxxxx form, in addition to escape sequence%xx.) )

For example:

Unescape ("it%27s%20me%21")   //Result: "It ' s me!"

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.