Manipulating functions and methods of strings in JavaScript

Source: Internet
Author: User
Tags truncated

1. String conversion toString (): You can convert any type of data to a string

var num= 19; 19

var mystr = num.tostring (); "19"

var mystr = String (num); "19"

var mystr = "" + num; "19

2, String split split (","): divides the string into bits multiple strings, split () the second parameter, which represents the maximum length of the returned string array

var str = "I,love,you,do,you,love,me"

var  strArr = myStr .split( "," ); // ["I", "Love", "You", "Do", "you", "love", "me"]; var strLim = myStr .split( "," , 3); // ["I", "Love", "You"];3. String cuttingSlice (i,j), substring (i,j)The truncated character should be in the second argument before a "i,j" var str = "I,love,you,do,you,love,me" var slice = str.slice (1,5); ", Lov" varsubstring= Str.substring (1,5); ", Lov" varsubstr= Str.substr (1,5); ", Love" substr (i,j) The second parameter represents " the maximum length of a truncated string " 4. Find the character at the given positioncharAt (8) var myStr = "I,love,you,Do,you,love,me" ; var theChar = myStr.charAt(8); // "o",同样从0开始5. Query substring Str.indexof ()

The first function:indexOf (), which starts from the beginning of the string, finds the corresponding coordinates, and cannot find the return-1. Following

var myStr =  "I,Love,you,Do,you,love,me" ; var index = myStr.indexOf( "you" );  // 7 ,基于0开始,找不到返回-1

The second function:lastIndexOf (), which starts at the end of the string and finds the corresponding coordinates returned, cannot find return-1. As follows:

var myStr =  "I,Love,you,Do,you,love,me" ; var index = myStr.lastIndexOf( "you" );  // 14  

Manipulating functions and methods of strings in JavaScript

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.