JavaScript string objects Common API functions Summary (connection, substitution, segmentation, conversion, etc.) _javascript tips

Source: Internet
Author: User

The examples in this article describe the common API functions for JavaScript string objects. Share to everyone for your reference, specific as follows:

1. Concat (str1,str2,)

Connection string

2. IndexOf (Str,start)

Returns the position of the first occurrence of STR in a string

var str = "Hello world";
Str.indexof ("Hello"); 0
str.indexof ("O", 5);//7
Str.indexof ("World");//-1

3. LastIndexOf (Str,start)

Returns the last occurrence of str in a string

var str = "Hello world";
Str.lastindexof ("Hello"); 0
str.lastindexof ("O", 3);//-1
str.lastindexof ("O", 5);//4

4. Replace (regexp/substr,replacement)

Substitute some characters in a string for another character, or replace one with a regular match

var str = "I is Allen.";
Str.replace ("is", "AM"); "I am Allen."


5. Slice (start,end)

Returns a fragment of a string

var str = "I am Jack.";
Str.slice (3,7); "M Ja"
str.slice (3);//"M Jack."
Str.slice (3,-3); "M Ja"

6. Split (Separator,limit)

Splits a string into substrings and then returns the result as an array of strings

var str = "Hello world";
Str.split (""); ["Hello", "World"]
str.split ("", 1); ["Hello"]

7. substr (Start,lenght)

Returns a string with a specified length starting at the specified position

var str = "How do I do?"
Str.substr (4,2); "
do
" str.substr (4); Str.substr (4,0); ""
str.substr (4,-1); ""
Str.substr (-3); "Do?"

8. SUBSTRING (start,end)

Returns a string at the specified position in a string object that contains the character at start, but does not contain a character at the end

var str = "How do I do?"
Str.substring (0,3); "How"

9. toLowerCase ()

Converts a string to lowercase

toUpperCase ()

Converts a string to uppercase

var str = "How do I do?"
Str.tolowercase (); "How did you do?"
Str.touppercase (); "How did you do?"

More readers interested in JavaScript-related content can view this site: "JavaScript in the JSON Operation tips Summary", "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills summary", " JavaScript animation effects and tips Summary, "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and Skills summary" and "JavaScript Mathematical operation Usage Summary"

I hope this article will help you with JavaScript programming.

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.