Common js processing string function sorting

Source: Internet
Author: User
Tags join regular expression split

function 1:split ()
Function: To store a string partition into an array using a specified delimiter

For example:

Str= "Jpg|bmp|gif|ico|png";
Arr=thestring.split ("|");
Arr is an array that contains the character values "JPG", "BMP", "GIF", "ico", and "PNG"

function 2:john ()

Function: Merges an array into a string using the separator of your choice
For example:

var delimitedstring=myarray.join (delimiter);
var mylist=new Array ("JPG", "BMP", "GIF", "ico", "PNG");
var portablelist=mylist.join ("|");
The result is jpg|bmp|gif|ico|png.

function 3:substring ()
Function: string interception, for example, to get "Minidx" from "minidxsearchengine" to use SUBSTRING (0,6)

function 4:indexof ()
Function: Returns the subscript of the first character in a string that matches a substring
For example:

var mystring= "JavaScript";
var w=mystring.indexof ("V");//w would be 2
var x=mystring.indexof ("S");//x would be 4
var y=mystring.indexof ("Script");//y'll also be 4
var z=mystring.indexof ("key");//z would be-1

Here's a "universal action function"--splice () method that handles arrays, which can be used to insert, delete, or replace elements of an array

1. Delete-use to delete elements. Two parameters, first argument (where you want to delete the first item), second argument (number of items to delete)
For example:


var arr=[1,2,3,4,5,6];
Arr.splice (2,3);//arr=[1,2,6]

2. Insert-Inserts any item element into the array at the specified position. Three parameters, first argument (starting position), second argument (0), third argument (inserted item)
For example:


Arr.splice (2,0, ' A ', ' B ', ' C ');//arr=[1,2,a,b,c,3,4,5,6]
Arr.splice (2,1, ' A ', ' B ', ' C '),//arr=[1,2,a,b,c,4,5,6] Note: The second parameter is x (for example: 1,2,3 ...) Deletes an X item from the start position and then inserts any item element

3. Replace-inserts any item element into the array at the specified position, deleting any number of items, and three parameters. First argument (starting position), second argument (number of deleted items), third argument (inserts any number of items)
For example:


Arr.splice (2,2, ' A ', ' B ');//1,2,a,b,5,6
Arr.splice (2,3, ' a ', ' B ');//1,2,a,b,6

Let's talk about the difference between slice,substr and substring.
First, they all receive two parameters, slice and substring receive the start and end positions (excluding the end position), and Substr receives the starting position and the length of the string to be returned. Look directly at the following example:


var test = ' Hello World ';
Alert (Test.slice (4,7)); o W
Alert (test.substring (4,7)); o W
Alert (TEST.SUBSTR (4,7)); O World

Note: The substring is the starting position in the smaller of two parameters, and the larger argument as the ending position.
Such as:


Alert (test.substring (7,4)); o W

Second, when the received parameter is a negative number, the slice will add the length of its string to the corresponding negative number, as a parameter; substr only the result of adding the first parameter to the length of the string as the first argument, and substring simply converts the negative parameters directly to 0. The test code is as follows:


var test = ' Hello World ';
Alert (Test.slice (-3)); Rld
Alert (Test.substring (-3)); Hello World
Alert (TEST.SUBSTR (-3)); Rld
Alert (Test.slice (3,-4)); Lo W
Alert (test.substring (3,-4)); Hel
Alert (TEST.SUBSTR (3,-4)); Empty string


Concat
Combines the text of two or more characters to return a new string.
var a = "Hello";
var B = ", World";
var C = A.concat (b);
alert (c);
c = "Hello,world"
IndexOf
Returns the index (search from left to right) of the first occurrence of a substring in a string. If there is no match, return-1.
var index1 = A.indexof ("L");
INDEX1 = 2
var index2 = A.indexof ("L", 3);
INDEX2 = 3
CharAt
Returns the character at the specified position.
var Get_char = A.charat (0);
Get_char = "H"
LastIndexOf
Returns the index (search from right to left) of the last occurrence of a substring in a string, or 1 if there are no matches.
var index1 = LastIndexOf (' l ');
INDEX1 = 3
var index2 = LastIndexOf (' l ', 2)
Index2 = 2
Match
Checks that a string matches the content of a regular expression, if no match returns NULL.
var re = new RegExp (/^\w+$/);
var is_alpha1 = A.match (re);
IS_ALPHA1 = "Hello"
var is_alpha2 = B.match (re);
IS_ALPHA2 = null
Substring
Returns a substring of a string in which the incoming argument is the starting and ending position.
var sub_string1 = a.substring (1);
Sub_string1 = "Ello"
var sub_string2 = a.substring (1,4);
Sub_string2 = "ell"
Substr
Returns a substring of a string in which the incoming argument is the starting position and length
var sub_string1 = a.substr (1);
Sub_string1 = "Ello"
var sub_string2 = a.substr (1,4);
Sub_string2 = "Ello"
Replace
Used to find a string that matches a regular expression, and then use a new string instead of a matching string.
var result1 = A.replace (Re, "Hello");
RESULT1 = "Hello"
var result2 = B.replace (Re, "Hello");
RESULT2 = ", World"
Search
Performs a regular expression-matching lookup. Returns the matching index value in the string if the lookup succeeds. otherwise return-1.
var index1 = A.search (re);
index1 = 0
var index2 = B.search (re);
Index2 =-1
Slice
Extracts part of the string and returns a new string (same as substring).
var sub_string1 = A.slice (1);
Sub_string1 = "Ello"
var sub_string2 = A.slice (1,4);
Sub_string2 = "ell"
Split
Make a string an array of strings by dividing the strings into substrings.
var arr1 = A.split ("");
ARR1 = [H,e,l,l,o]
Length
Returns the length of a string, called the length of a string, that is the number of characters it contains.
var len = A.length ();
Len = 5
toLowerCase
Converts the entire string into lowercase letters.
var lower_string = A.tolowercase ();
lower_string = "Hello"
toUpperCase
Converts the entire string to uppercase.
var upper_string = A.touppercase ();
upper_string = "HELLO"

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.