Javascript--string

Source: Internet
Author: User

In JS, single and double quotation marks can be used as delimiters of strings, especially in the described string.

When double quotation marks are to be displayed, single quotation marks can be used as delimiters of the string directly

Eg: ' This was a ' hot ' dog ' or ' this is a \ ' hot\ ' dog '-the latter needs to add escape characters

There are two common methods of turning string types to other types:

1. ToString () method

var num = 33;

var result = num.tostring ();

document.write (typeOf (result));

2.String () method

var result = String (num);

Alert (typeOf (result));

Any type of data can be converted to a string type of data through the ToString () method, where the result of the array being converted to a string is

The result of the join () method of the array is consistent, that is, the string of elements of each array connected by commas

How to create a string and other ways to do it

var astring = ' This is a string ';

var bstring = new String (' string ');

CharAt () returns one of the characters of a string by subscript (the length of the 0~ string-1, similar to an array) gets the top character

Alert (Astring.charat (8));

Ways to get the length of a string

var length = Astring.length;

alert (length);

Search () finds the string, returns the subscript of the string in the target string, returns 1 if not found

Alert (Astring.search (' I '));

SUBSTRING () intercepts the string, contains the starting subscript, does not contain the end subscript, if the end subscript is not written, the output starts with the underlying value and later,

Parameter cannot be a negative

Alert (Astring.substring (5, 9));

The slice () result is the same as the search (), but slice can use a negative number-1 for the end character and so on, 2 is the reciprocal

A second character

Alert (Astring.slice (-5,-3));

SUBSTR () The second parameter is the number of inclusions, not including negative numbers

alert (ASTRING.SUBSTR);

The use of the split () method saves the string in the array by dividing it into substrings segmentation the specified word.

The first argument is the character used for the slice, and the second argument is the string (array) length (which can be omitted)

document.write (Astring.split ("', 2)"); ---According to the space, length is two

The IndexOf () method returns the position of the first occurrence of a specified string value in a string.

var index = astring.indexof (' s ');

alert (index);

Javascript--string

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.