JavaScript intercepts strings multiple ways

Source: Internet
Author: User

To intercept the last character.

The code is as follows Copy Code

Str.charat (str.length–1)

Method Two: Using the Substr method under the String object

The code is as follows Copy Code
Str.substr (str.length-1,1)

Method Three: Using the Split method under String object

The code is as follows Copy Code

var str = "123456″;
Spstr = Str.split ("");
SPSTR[SPSTR.LENGTH-1];

Commonly used interception using substring () or slice ()

Function: Split ()
Function: To store a string partition into an array using a specified delimiter
Example:

The code is as follows Copy Code
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: John ()
Function: Merges an array into a string using the separator of your choice
Example:

The code is as follows Copy Code
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: substring ()
Function: string interception, for example, to get "Minidx" from "minidxsearchengine" is necessary to use

The code is as follows Copy Code
SUBSTRING (0,6)

Function: IndexOf ()
Function: Returns the subscript of the first character in a string that matches a substring

The code is as follows Copy Code

var mystring= "JavaScript";
var w=mystring.indexof ("V"); W'll 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'll be-1


Support for Chinese

The code is as follows Copy Code
function substr (str, len) {
if (!str | |!len) {return ';}
Expected count: Chinese 2 bytes, English 1 bytes
var a = 0; Loop count
var i = 0; Temporary string
var temp = ';
for (i=0;i<str.length;i++)
{
if (Str.charcodeat (i) >255)
{//increase by 2 as expected count
a+=2;
}
Else
{
a++;
//If the length of the count is greater than the length of the limit, return the temporary string directly
if (a > Len) {return temp;} Adds the current content to the temporary string
Temp + + Str.charat (i);
///If all Single-byte characters are returned directly to the source string
return str;
}

For more details please see: http://www.111cn.net/wy/js-ajax/36384.htm

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.