A brief description of common JavaScript string methods

Source: Internet
Author: User
Tags string methods

Website Source: http://www.html-js.com/article/ Js-rookie-in-the-rookie-to-start-learning-to-fly-the-commonly-used-string-method-in-javascript

indexOf (str) returns the position of the first occurrence of the argument string in the string (left-to-right search, and the following table starts at 0). If there is no match, return-1

Enter code herevar str="Gudaochuan";var index1=str.IndexOf(C);//result is 5var index2=str.IndexOf (,3" ;//starting at index position 3, looking right, the result is 7var index3=str. Indexof ( "I" ;//result is-1, character not found Ivar index4=str< Span class= "token punctuation". indexof ( "DAO" ,1) ;//result is 2             

Here are the results of the above code execution, which can be self-tested

Note: indexOf () is case sensitive!

charAt (Index) returns the character at the specified position.

var result=str.charAt(0);//result的结果是"g"

Here are the results of the execution

lastIndexOf (str) returns the index (right-to-left search) of the last occurrence of the argument string in the string, or 1 if there is no match

enter code herevar result1=str.lastIndexOf("a");//结果是8var result2=str.lastIndexOf("a",3);//从索引位置3开始,往左查找,结果是3

Here are the results of the execution

substring (start,[end]) finds a string at the specified index position and returns it. Returns the string from the position of the first argument to the previous one at the position of the second parameter. The first parameter is required, and the second parameter can be written without a write. If the second argument is not written, the end position is the last one.

enter code herevar result1=str.substring(5);//输出结果是"chuan"var result2=str.substring(5,9);//输出结果是"chua"

Execution Result:

Substr[begin,[length]] returns a substring of a string in which the incoming parameter is the starting position and length. The first parameter is required, and the second parameter can be written without a write. If the second argument is not written, it indicates the end position to the last (this is the same as the substring method)

enter code herevar result1=str.substr(5);//输出结果是"chuan"var result2=str.substr(5,4);//输出结果是"chua"

Execution Result:

toUpperCase () and *tolowercase () * toUpperCase () turns the entire string into uppercase toLowerCase () converts the entire string into lowercase letters

enter code here//toUpperCase()var result1=str.toUpperCase();//输出结果是GUDAOCHUAN//toLowerCase()var result2=result1.toLowerCase();//输出结果是gudaochuan

Execution Result:

The replace () string finds and replaces the method with a powerful function. Need to match regular expressions

enter code herevar result=str.replace(/[o]/g,"---");//结果是guda---chuan

Execution Result:

Search (REG) is used to find the position of the character, and indexof () much like, he is a bit more than indexof is a parameter can be a regular expression

enter code herevar result=str.search(/[o]/);//执行结果是4

Execution Result:

split (Separator,[limit]) separates a string with the established character or regular delimiter, and returns the result as an array. If there is no delimiter specified, the default is a comma, which is the delimiter.

Enter code herevar str="2014-12-12 11:11:11";  var ary1=str.  Split(/[-:]/);//output is ["" "," "" "," "" "," one "," one "," one "]var ary2=str.  Split(/[-:]/,2);//output is [","]       

Execution Result:

Match (REG) This method is very powerful and needs to be implemented in conjunction with the regular. He is using the regular parameter to save the contents of the regular match to the array returned if there is no match to return null.

Enter code herevar reg=/^[a-z]+$/;var str1="Gudaochuan";var str2="0123456789";var str3= "Gu012dao-chuan" ; var result1=str1. Match (Reg;// The result is an array of classes that holds the var result2=str2 match (Reg;//result is nullvar result3=str3match (Reg;//result is null            

Execution Result:

concat () combines text of two or more characters to return a new string. Not much to use. We usually use + to splice

enter code herevar a="hello";var b=":呵呵~"var result=a.concat(str,b);//执行输出“hellogudaochuan:呵呵~”

Execution Result:

The slice () function is the same as substring () and is not used much.

charCodeAt (Index) The Unicode code of the character that specifies the index position

enter code herevar num=str.charCodeAt(1);//结果是117

Execution Result:

A brief description of common JavaScript string methods

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.