JavaScript Classic Instance-string

Source: Internet
Author: User

One, the string

1] ' This is a string ', which is the direct amount of strings, new string (' This is a string '), which is a string object, the string object can be used in the string object with the properties and methods, the direct amount of the "surface" can also be used, but the essence is first turned into an object, Discard the object after processing is finished, and return the result. Such as:

1 var city =new String ("St. Louis"); 2 var lccity = City.tolowercase (); // output St. Louis

2] The connection string can be concat by the operator "+" or by using a string object with its own function, such as:

var oracle = "Oracle"+ = "SQL"; // Output Oracle SQL /*  */var oracle = "". Concat ("Oracle", "SQL"); // Of course, you can add more parameters

3] String comparisons are generally made by the operator "= =", "<", ">", and a method of a string object Localecompare can also be compared, such as:

var f1 = "grape"; var i = F1.localecomparecompare (F2);//return -1/* same as 0, less than-1, greater than 1 (f1 contrast F2) */

4] Finding substrings in a string can be indexof by the built-in object of string, the position of the substring is returned, if LastIndexOf can be counted from right to left, and of course there are many other ways, which is just one of them. An example of how to index is as follows:

"This apple is my Apple". LastIndexOf ("Apple"); // return value"This apple is my Apple". LastIndexOf ("Apple", 12); // returns the value 5"This apple is my Apple". LastIndexOf ("Apple", 3); // return value-1 (not found)

5] Split the string, we should all know, substr, substring, there is split, with these methods, with IndexOf can extract the string. The example is not to be lifted.

6] Check for non-empty strings, using typeof, ValueOf, and length lengths. typeof detects the basic type of data, valueof is the method of all objects, not limited to string, returns the base value of the calling object-it is also used because if the string is not determined to exist, length will be an error, and if the string is not a direct amount, Instead of a string object that is new, typeof also returns the object type, but generally in our actual application, it is very rare to have such an indeterminate thing, so the general omission of valueof can achieve the purpose.

7] Remove whitespace at the end of the string, you can use the string object with the Replace method, with a regular blank are replaced with "", but ECMAScript5 added a trim method, you can automatically remove the blank, but IE8 does not support, you can encapsulate one, such as:

if (typeof String.Trim = = "undefined")  {function()      {return  this. Replace (/(^\s) | ( \s*$)/g, "");  }  }

If there is no Trim method inside the string object, then a method is added to the string prototype that can be used. In addition, the left and right blanks can be removed separately, trimleft,trimright.

(not to be continued ...) )

JavaScript Classic Instance-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.